Thread: help with sorting numbers

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    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()
    {
    }

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    18
    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 View Post
    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 View Post
    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 View Post
    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 -

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Adak View Post
    You need two nested loops to sort a 1D array. something like this:
    Not if you use Gnome Sort

    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    hey I ran the code on that it won't print out the last number if I use 10 - 1

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Correct!

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

    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.
    Last edited by Adak; 02-06-2010 at 02:30 PM.

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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]);
    }

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Adak View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. Sorting numbers in a file
    By pxleyes in forum C Programming
    Replies: 19
    Last Post: 04-15-2004, 06:17 AM
  3. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM