# help with sorting numbers

• 02-06-2010
artistunknown
help with sorting numbers
I have this program I've been working on in this c book I have and it wants me to sort numbers in ascending or descending order I think I can figure out the descending anyway I know that I might have to use an if statement but maybe not I have this worksheet from when I took c# that has the code to sort in descending order so I think I can figure that out here is my lame attempt it doesn't sort I'm trying to well I know what it does it just prints 10 10 times I know I have to compare the array 0 with array 1 then which ever is bigger then print that first I think but I can't figure out what to do heres my code

Code:

``` #include <stdio.h> #include <stdlib.h> void SortAscending(void); void SortDescending(void); int iArray[9]; int x; int main() {         int iChoice = 0;                 for (x = 0; x < 10; x++)         {         printf("\n\tEnter ten numbers: ");         scanf("%d",&iArray[x]);         }         system("cls");         printf("\n\tEnter sorting method\n");         printf("1\tAscending order\n");         printf("2\tDescending order\n");         printf("Enter number: ");         scanf("%d",&iChoice);         if (iChoice == 1)                 SortAscending();         else if(iChoice == 2)                 SortDescending(); } void SortAscending() {         for(x = 0; x < 10; x++)                 if (iArray[x] >= iArray[x]+x)                         printf("%d\n",iArray[x]);                 else                         printf("%d\n",iArray[x]+x);         } void SortDescending() { }```
• 02-06-2010
ForzaItalia2006
Hey,

There are some issues with your code and I would recommend to also read a book about algorithms, especially sorting algorithms, because that what you do has nothing to do with sorting an array ... Please find some of the comments to your code below.

Quote:

Originally Posted by artistunknown
Code:

```      int iArray[9];       ...         for (x = 0; x < 10; x++)         {         printf("\n\tEnter ten numbers: ");         scanf("%d",&iArray[x]);         }```

You create an array with 9 elements, but you finally write 10 elements into it. Your app might not crash immediately, but you should not do that ...

Quote:

Originally Posted by artistunknown
Code:

`system("cls");`

The use of the system(3) library call is very system-dependent. IIRC, "cls" is a Windows command, but it is not available on Linux. Though, your code isn't really portable. For hints, you might want to read this FAQ: Cprogramming.com FAQ > Clear the screen?

Quote:

Originally Posted by artistunknown
Code:

```void SortAscending() {         for(x = 0; x < 10; x++)                 if (iArray[x] >= iArray[x]+x)                         printf("%d\n",iArray[x]);                 else                         printf("%d\n",iArray[x]+x);         }```

As I already said above, that is NOT sorting. Assume you have an array of 3 with the following values: [5 - 10 - 1]. You will never get the 1 at the first position assuming ascending order.

Here are some sorting algorithms which you could look up in the web:

- Andi -
• 02-06-2010
Code:

```void SortAscending() {         for(x = 0; x < 10; x++)                 if (iArray[x] >= iArray[x]+x)                         printf("%d\n",iArray[x]);                 else                         printf("%d\n",iArray[x]+x);         } Make your array size 10 (or whatever number), and then use x < 10. Here you made your array size only 9, so in C with zero based arrays, your x variable will run beyond the top of the array on the last loop. You need two nested loops to sort a 1D array. something like this: for(i = 0; i < 9-1; i++) {   for(j = i+1; j < 9; j++) {     if(a[i] > a[j]) {  //change to < for descending sort, and no = is needed       temp = a[i];       a[i] = a[j];       a[j] = temp;     }   } } Change the 9 to 10 if you change the array size to 10, in for loops.```
That will get your sorts going, although this is a slow sorter on large arrays.
• 02-06-2010
artistunknown
hey thanks guys I guess learning sorting algorithms is in data structures classes and I'm not that advanced yet but I will get a book on data structures I will also read the faqs you suggested
• 02-06-2010
iMalc
Quote:

Originally Posted by Adak
You need two nested loops to sort a 1D array. something like this:

Not if you use Gnome Sort :p

I can't fathom what misunderstanding of C led one to write:
Code:

`if (iArray[x] >= iArray[x]+x)`
Everyone who can use any programming language should be able to come up with some kind of runimentry way of sorting an array, even if it's just picking two items at random, and swapping them if they are out of order, and continuing until the data is sorted.
You mustn't know the language well enough yet.
• 02-06-2010
artistunknown
Yeah I have to admit my code was pretty lame this is the finished product from what adak taught me

Code:

``` #include <stdio.h> #include <stdlib.h> void SortAscending(void); void SortDescending(void); int iArray[9]; int main() {         int x;         int iChoice = 0;                 for (x = 0; x < 10; x++)         {         printf("\n\tEnter ten numbers: ");         scanf("%d",&iArray[x]);         }         system("cls");         printf("\n\tEnter sorting method\n");         printf("1\tAscending order\n");         printf("2\tDescending order\n");         printf("Enter number: ");         scanf("%d",&iChoice);         if (iChoice == 1)                 SortAscending();         else if(iChoice == 2)                 SortDescending(); } void SortAscending() {         int i;         int j;         int temp;         for(i = 0; i < 10; i++)         {                 for(j = i+1; j < 10; j++)                 {                         if(iArray[i] > iArray[j])                         {                                  temp = iArray[i];                                 iArray[i] = iArray[j];                                 iArray[j] = temp;                                                             }                   }                 printf("%d\n",iArray[i]);         }         } void SortDescending() {         int i;         int j;         int temp;         for(i = 0; i < 10; i++)         {                 for(j = i+1; j < 10; j++)                 {                         if(iArray[i] < iArray[j])                         {                                  temp = iArray[i];                                 iArray[i] = iArray[j];                                 iArray[j] = temp;                                                             }                   }                 printf("%d\n",iArray[i]);         } }```
after I posted I thought maybe I should use a nested loop but I would have never guessed this code yeah I don't know the language good enough
• 02-06-2010
One slight correction to make, ArtistUnknown:

Code:

```         for(i = 0; i < 10-1; i++)  //change from < 10 to < 10-1         {                 for(j = i+1; j < 10; j++)                 {                         if(iArray[i] < iArray[j])                         {                                  temp = iArray[i];                                 iArray[i] = iArray[j];                                 iArray[j] = temp;                                                             }                   }                 printf("%d\n",iArray[i]);         }```
Because j is always one starting one element higher in the array than i. On the last time through the loop, i needs to be one less than the last element to keep j from going outside the top of the array.
• 02-06-2010
artistunknown
ok thanks should i = 1 or i = 0 in that loop when I run the code the way I have it it works but I will change it
• 02-06-2010
artistunknown
hey I ran the code on that it won't print out the last number if I use 10 - 1
• 02-06-2010
Correct!

Did you try coaxing it with candy? Buying it dinner? :p :p

The thing is, it MIGHT crash the program if you don't have 10-1 for the stopping point for i. You can easily see that, on the last loop, j will start with i+1, and j needs to be < 10. The thing is, without it, you're also sorting into your numbers, God only knows what.

So i needs to be < 9.

You'll have to add another printf() statment after the last brace } to get the last element.
• 02-06-2010
artistunknown
oh yeah I think I remember something like that in c# class sorry I know you guys must get bored with answering stupid questions on here
• 02-06-2010
artistunknown
does this look better

Code:

``` #include <stdio.h> #include <stdlib.h> void SortAscending(void); void SortDescending(void); int iArray[9]; int main() {         int x;         int iChoice = 0;                 for (x = 0; x < 10; x++)         {         printf("\n\tEnter ten numbers: ");         scanf("%d",&iArray[x]);         }         system("cls");         printf("\n\tEnter sorting method\n");         printf("1\tAscending order\n");         printf("2\tDescending order\n");         printf("Enter number: ");         scanf("%d",&iChoice);         if (iChoice == 1)                 SortAscending();         else if(iChoice == 2)                 SortDescending(); } void SortAscending() {         int i;         int j;         int temp;         for(i = 0; i < 9; i++)         {                 for(j = i+1; j < 10; j++)                 {                         if(iArray[i] > iArray[j])                         {                                  temp = iArray[i];                                 iArray[i] = iArray[j];                                 iArray[j] = temp;                                                             }                   }                 printf("%d\n",iArray[i]);         }         printf("%d\n",iArray[i]); } void SortDescending() {         int i;         int j;         int temp;         for(i = 0; i < 9; i++)         {                 for(j = i+1; j < 10; j++)                 {                         if(iArray[i] < iArray[j])                         {                                  temp = iArray[i];                                 iArray[i] = iArray[j];                                 iArray[j] = temp;                                                             }                   }                 printf("%d\n",iArray[i]);         }         printf("%d\n",iArray[i]); }```
• 02-06-2010
iMalc
Quote:

Originally Posted by Adak
The thing is, it MIGHT crash the program if you don't have 10-1 for the stopping point for i. You can easily see that, on the last loop, j will start with i+1, and j needs to be < 10. The thing is, without it, you're also sorting into your numbers, God only knows what.

So i needs to be < 9.

You'll have to add another printf() statment after the last brace } to get the last element.

Actually it will work fine with just 10. In fact if the printf wasn't there then it would work fine if it were i<10000. The reason being that it wont go into the inner loop (which is the only place the array contents would be looked at) unless j < 10 is true, which it wont be if j is i+1 and i is 9 or greater.