Need a for loop to sort an array

This is a discussion on Need a for loop to sort an array within the C Programming forums, part of the General Programming Boards category; I have an array of numbers that I read in from the user. I know that they have been read ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    36

    Need a for loop to sort an array

    I have an array of numbers that I read in from the user. I know that they have been read in correctly because I am able to print them. Only problem is I can't sort my array. Can someone please help?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
        char fname[256];
        FILE* fin;
        int n; //Size of the set allowed digits
        int k; //Length of the target numbers
        int numbers[10];
        int i, j;
        int temp;
       
        printf("Enter the name of the file to read from: \n");
        scanf("%s", fname);
        
        fin = fopen(fname, "r");
        
        //Check to make sure the file exists and opens correctly
        if(fin == NULL)
        {
            printf("Unable to open the file %s\n", fname);
            system("PAUSE");
            return 0;   
        } 
            
        /*Read in the two integers on the first line of the text file*/
        //How many numbers to read in
        fscanf(fin, "%d ", &n);  
    
        //0 in the file means stop!
        if (n == 0)
            return;
        
        //How many digits for each number to be
        fscanf(fin, "%d ", &k); 
        
         //Make sure array is empty
        for (i = 0; i <= n; i++)
        {
            numbers[i] = 0;
        }
            
        //Second line
        for(j = 0; j < n; j++)
            fscanf(fin, "%d \n", &numbers[j]);
    
        //Sort the array
        for (i = 0; i < n; i++)
        {
            for (j = i + 1; j < n; j++)
            {
                if (numbers[i] > numbers[j])
                {
                    temp = numbers[i];
    				numbers[i] = numbers[j];
    				numbers [j] = temp;
    				printf("We are in the sort loop now \n");
    				printf("%d \n", numbers[i]);
    			}
    		}
        }
        
        fclose(fin);
        
        system("PAUSE");
        return 0;   
    }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    What if the number that is read in to "n" is 11?

    Other than that, I can see nothing wrong with this. Why do you think you are not sorted? It looks like a perfect bubble sort to me. Add a loop at the end that dumps the array (printf() that is).

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    I presume you're looking at the output “We are in the sort loop now” along with the int you're printing out there to determine that it's not being sorted? That's not the right place to look. You're just watching the bubbling action there. Get rid of those printf() calls, and, right after your sort loop, print out all the elements of the array. You might be pleasantly surprised.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    36
    cas---where exactly in the code should I try to print?

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    36
    nevermind...i figured it out. You guys were correct. I had it right all along. Next question - how do I recursive call a function to take the number I read in, say 1, 3, 6 and make all 2-digit possible combinations...i.e. 11, 13, 16, 31, 33, 36, 61, 63, and 66?

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    How do you think you would do it (show some code, dude)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-06-2009, 11:27 AM
  2. Insertion Sort on Array of Structs
    By n0r3gr3tz in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 08:28 AM
  3. Using unix command line to sort random array
    By lostmyshadow in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 06:14 PM
  4. New Sort an Array of Classes
    By ajpeters in forum C++ Programming
    Replies: 11
    Last Post: 09-06-2005, 06:23 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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