Sorting array output problem

This is a discussion on Sorting array output problem within the C Programming forums, part of the General Programming Boards category; Ive been asked to write a program that asks the user to input a series of numbers, then reads the ...

  1. #1
    Unregistered
    Guest

    Sorting array output problem

    Ive been asked to write a program that asks the user to input a series of numbers, then reads the numbers back to the user in numerical order. Ive come up with the following code but i get a large negative integer as the sorted output. Can anyone see why?

    Code:
    #include <stdio.h>
    #define k 10 /* used for setting the array size */
    int main()
    {
    	int a[k], n, p, j, small_j, smallest;
    	char proceed='Y';
    	printf("Program to sort up to %d integer numbers\n\n", k);
    	while(proceed == 'Y' || proceed == 'y')
    	{
    		printf("\nHow many numbers would you like to sort? ");
    		if(scanf("%d", &n) == 1 && n>0 && n<=k)
    		{
    			/* An acceptable value for n was read */
    			printf("\n\nThe program will sort %d integers\n", n);
    			
    			/* Read the n values into the array */
    			printf("\nType in the numbers - integers\n\n");
    			for(j=0; j<n; j++) scanf("%d", &a[j]);
    
    			/* Display the values back to the user */
    			printf("\n\nThe numbers you have input are\n\n");
    			for(j=0; j<n; j++) printf("%d ", a[j]);
    
    			/* Start the sorting process by swapping each element with the smallest 
    			remaining elements */
    			/* Use p to count the sorted elements as build them */
    			/* Note p=k-1 */
    			for(p=0; p<n-1; p++) /* main loop for sort */
    			{
    				/* Find smallest of the remaining n-p unsorted numbers */
    				/* Start the scan by designating the first of the unsorted elements
    				as smallest found so far */
    				smallest = a[p];
    				small_j = p; /* Note its position */
    
    				/* By varying j, scan through the remaining n-p-1 unsorted 
    				elements to see if any is smaller */
    				for(j=p+1; j<n; j++)
    					if(a[j] < smallest)
    					{
    						smallest = a[j]; /* Update record of smallest element so far */
    						small_j = j; /* Note its position */
    					}
    
    					/* Scan finished */
    
    					/* Enlarge the sorted list and reduce the unsorted list by 
    					swapping the element at position p with the smallest element */
    
    					a[small_j] = a[p];
    					a[p] = smallest;
    
    			}	/* End of main loop */
    
    			/* Sorting process is now complete so now display the sorted values to the user */
    			printf("\n\nThe sorted numbers are\n\n");
    			for(j=0; j<n; j++);printf("%d ", a[j]);
    		}
    		
    		else printf("\n\nInvalid input\n\n");	/* Unacceptable value was typed when 
    												user was inputting n */
    
    		/* Find out if the user wants another sort to be performed */
    		printf("\n\nWould you like to try to sort another set of numbers?(Y or N) ");
    		proceed=getchar();
    			
    		while (proceed !='Y'&&proceed !='y'&&proceed !='n'&&proceed !='N')
    			proceed=getchar();
    			
    	}
    	
    	/*The user no longer wishes to sort numbers so the program can stop*/
    		
    	printf("\nThis program is now ending\n\n");
    	return 0;
    }

  2. #2
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    gr.........remove the 400 lines of commenting before you post it, so everyone can read the code better..

  3. #3
    Unregistered
    Guest
    Sorry, thought it may explain my thought processes better, clean code below

    Code:
    #include <stdio.h>
    #define k 10
    int main()
    {
    	int a[k], n, p, j, small_j, smallest;
    	char proceed='Y';
    	printf("Program to sort up to %d integer numbers\n\n", k);
    	while(proceed == 'Y' || proceed == 'y')
    	{
    		printf("\nHow many numbers would you like to sort? ");
    		if(scanf("%d", &n) == 1 && n>0 && n<=k)
    		{
    			printf("\n\nThe program will sort %d integers\n", n);
    			
    			printf("\nType in the numbers - integers\n\n");
    			for(j=0; j<n; j++) scanf("%d", &a[j]);
    
    			printf("\n\nThe numbers you have input are\n\n");
    			for(j=0; j<n; j++) printf("%d ", a[j]);
    
    			for(p=0; p<n-1; p++)
    			{
    				smallest = a[p];
    				small_j = p;
    
    				for(j=p+1; j<n; j++)
    					if(a[j] < smallest)
    					{
    						smallest = a[j];
    						small_j = j;
    					}
    
    					a[small_j] = a[p];
    					a[p] = smallest;
    
    			}
    
    			printf("\n\nThe sorted numbers are\n\n");
    			for(j=0; j<n; j++);printf("%d ", a[j]);
    		}
    		
    		else printf("\n\nInvalid input\n\n");	
    
    		printf("\n\nWould you like to try to sort another set of numbers?(Y or N) ");
    		proceed=getchar();
    			
    		while (proceed !='Y'&&proceed !='y'&&proceed !='n'&&proceed !='N')
    			proceed=getchar();
    			
    	}
    		
    	printf("\nThis program is now ending\n\n");
    	return 0;
    }

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    There's nothing wrong with commenting your code, it is even very useful to comment code. But try not to translate the C code in natural language.

    Don't do things like this:

    /* An acceptable value for n was read */
    printf("\n\nThe program will sort %d integers\n", n);

    But this

    /* Start the sorting process by swapping each element with the smallest remaining elements */

    kind of comment is OK. Comments should not explain code, but be supply additional information.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    > for(j=0; j<n; j++);printf("%d ", a[j]);
    See that ; between the loop and the printf?

    Remove it

    Oh, to stop the massive indent, use spaces for indentation, not hard tab characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. array sorting problem?
    By vutek0328 in forum C Programming
    Replies: 14
    Last Post: 09-12-2006, 12:07 PM
  3. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 01:04 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Problem with sorting an array
    By lostminds in forum Game Programming
    Replies: 2
    Last Post: 04-24-2002, 10:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21