I have the following array below apart from it having more data and its all stored in a string array. how would I sort it so that It goes from highest to lowest.

col 0 col 1
example 0 90.87
example 1 100.87
example 2 873.88
example 9 88.98

to

example 2 873.88
example 1 100.87
example 0 90.87
example 9 88.98

???

Strings in C are compared, using the include file string.h, and the strcmp() function. Here's how it works:

The value returned by strcmp() tells whether the strings are:

0: means they are equal
>0: means string1 is > than string2
<0: means string1 is < string2

int variable = strcmp(string1, string2);

or

if((strcmp(string1, string2)) > 0) {
   //string1 was > string2 (see how the >'s match up)
}

BTW, example 9 is going to be at the top of your list, if it's sorted in descending order.

Do you have some sorting code? If not, post up your code, and let's get you sorting. ;)

Edited 6 Years Ago by Adak: n/a

to make a char array
of 2 columns containing strings 49 characters long and
10 rows contaning strings also 49 characters long.

is this correct char ArrayName[10][50][2][50] ??

to make a char array
of 2 columns containing strings 49 characters long and
10 rows contaning strings also 49 characters long.

is this correct char ArrayName[10][50][2][50] ??

what i get is you want a char array that have 10 rows and each row contains a 49 char string...
then it could be done by simply
char charname [10][50]

Never Mind I read the task wrong as I thought it all had to be in one array but I'm allowed as many as I need.

Cheers for the help though

It may not be the prettiest but I've done most of my task. All I need to do now is too display the cheapest item. How would I do this??

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

void input();
void view();

char 	item[10][50]; // array of strings
float  	price[10]; // array of the prices
float  	total=0; // running total
int     edit=0; // to see if data has been inputed

int main()
{
    int menu=0;
    
    system("cls");
    printf("welcome to the main menu\n");
    do
    {
          system("cls");
          printf("please choose one of the following and type the number below and press enter:\n");
          printf("1 - Input 10 list shopping list\n2 - View 10 list shopping list\n");
          fflush(stdin);
          scanf("%d",&menu);
          switch(menu)
          {
              case 2:
                   if(edit==1)
                   {
                        view();
                        break;
                   }
                   
                   else if(edit==0)
                   {
                       printf("You need to input data before you can view the list !!!\n");
                       sleep(3000);
                       break;
                   }
              case 1:
                   input();
                   break;
          }
    }while(menu!=1 || 2);
    
    

          
}


void view()
{
     int loop;
     int menu;
     
     system("cls");
     for (loop=0;loop<10;loop++)
     {
         printf("%s, %0.2f\n",item[loop],price[loop]);
     }
     printf("all of these items cost %0.2f",total);
     printf("\n\n"); 
     do
     {
         printf("when your ready to go to the main menu\ntype 1 below and press enter\n");
         scanf("%d",&menu);
     }while(menu!=1);
     
}


void input()
{
		float   temp_price=0; // temp price input
		int 	pnum=0; // product number
		int     correct; // is this correct

	
        do
        {
            do
            {
                system("cls");
                printf("Please enter the name of product %d of 10:\n",pnum+1);
                fflush(stdin);
                gets(item[pnum]);
                printf("Please enter the price of product %d of 10:\n",pnum+1);
                fflush(stdin);
                scanf("%f",&temp_price);
                printf("is the name of your product %s\n",item[pnum]);
                printf("and does it cost %0.2f\n",temp_price);
                printf("Please write '1' if correct and '0' if incorrect\n");
                fflush(stdin);
                scanf("%d",&correct);
                edit=1;
            }while (correct!=1);
        
        total=total+temp_price;
        price[pnum] = temp_price;
        pnum++;		
        }while(pnum!=10);
        system("cls");
}

Please read this about fflush() And read this about gets() We also recommend you not use the system() call to clear the screen. It's annoying to the users and it's also not standard.

How would YOU find the lowest price item?

Perhaps, in a loop, you'd compare each item, and if it was the lowest priced item so far, set that value to be the lowPrice varible's value?

Well, two reasons:

1) he has an array, already, but no linked list. More efficient here, to just use what he has.


2) Does he know how to use a linked list? I don't know. My guess is no, he doesn't. If you don't know how to find the minimum value in an array, you probably don't know how to work with a linked list yet, either.

But it could be done with a linked list! ;)

This question has already been answered. Start a new discussion instead.