Thread: need help sorting integer list using array

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    need help sorting integer list using array

    here is the contents of my data file:
    6 67.00 F 11.00 C
    7 73.00 F 13.00 C
    8 82.00 F 17.00 C
    9 79.00 F 10.00 C
    10 79.00 F 12.00 C
    11 79.00 F 9.00 C
    12 72.00 F 14.00 C
    13 52.00 F 6.00 C
    14 45.00 F 0.00 C
    15 60.00 F 4.00 C
    26 26.11 C 39.00 F
    27 22.22 C 52.00 F
    28 11.11 C 32.00 F
    29 7.22 C 25.00 F
    30 15.56 C 43.00 F
    1 60.00 F 15.00 C
    2 55.00 F 10.00 C
    3 71.00 F 14.00 C
    4 63.00 F 12.00 C
    5 42.00 F -5.00 C
    21 19.44 C 45.00 F
    22 22.78 C 50.00 F
    23 27.78 C 42.00 F
    24 26.11 C 49.00 F
    25 26.11 C 59.00 F
    16 15.56 C 35.00 F
    17 12.78 C 25.00 F
    18 21.67 C 60.00 F
    19 17.22 C 57.00 F
    20 5.56 C 30.00 F
    first i had to convert all of the Celcius measurements to Farenheight which i completed easily

    now i am trying to sort this list of data in ascending order using the numbers 1-30 from the first column of the data file above

    here is my code...

    Code:
    int main( void )
    {
       int x;
       int day[30];
       float htemp;
       float ltemp;
       char hmeasure;
       char lmeasure;
    
       FILE *infile;
       infile = fopen(INFILE, "r" );
    
       FILE *outfile;
       outfile = fopen(OUTFILE, "w" );
    
       while( fscanf(infile, "%i %f %c %f %c", &day[x], &htemp, &hmeasure, &ltemp, &lmeasure) == 5 )
       {
          switch(hmeasure)
          {
    	 case 'F':
    	    htemp = htemp;
    	    break;
    	 case 'C':
    	    htemp = ((htemp*1.8)+32);
                hmeasure = 'F';
    	    break;
          }
    
          switch(lmeasure)
          {
    	 case 'F':
    	    ltemp = ltemp;
    	    break;
    	 case 'C':
    	    ltemp = ((ltemp*1.8)+32);
    	    lmeasure = 'F';
    	    break;
          }
    
         
          void sort(int day[], int n);
    
          fprintf(outfile, "%i %4.2f %c %4.2f %c\n", day, htemp, hmeasure, ltemp, lmeasure);
    
       }
    
       fclose (infile);
       fclose (outfile);
    
    return 0;
    }
    
    
    
    void sort(int day[], int n)
    {
       int k, j, m;
       double hold;
    
       for (k=0; k<=n-2; k++)
       {
          m=k;
          for (j=k+1; j<=n-1; j++)
          {
    	 if (day[j] < day[m])
    	    m = j;
          }
          hold = day[m];
          day[m] = day[k];
          day[k] = hold;
       }
    
       return;

    and here is the result of my code:

    7218 67.00 F 51.80 F
    7218 73.00 F 55.40 F
    7218 82.00 F 62.60 F
    7218 79.00 F 50.00 F
    7218 79.00 F 53.60 F
    7218 79.00 F 48.20 F
    7218 72.00 F 57.20 F
    7218 52.00 F 42.80 F
    7218 45.00 F 32.00 F
    7218 60.00 F 39.20 F
    7218 79.00 F 39.00 F
    7218 72.00 F 52.00 F
    7218 52.00 F 32.00 F
    7218 45.00 F 25.00 F
    7218 60.01 F 43.00 F
    7218 60.00 F 59.00 F
    7218 55.00 F 50.00 F
    7218 71.00 F 57.20 F
    7218 63.00 F 53.60 F
    7218 42.00 F 23.00 F
    7218 66.99 F 45.00 F
    7218 73.00 F 50.00 F
    7218 82.00 F 42.00 F
    7218 79.00 F 49.00 F
    7218 79.00 F 59.00 F
    7218 60.01 F 35.00 F
    7218 55.00 F 25.00 F
    7218 71.01 F 60.00 F
    7218 63.00 F 57.00 F
    7218 42.01 F 30.00 F
    as you can see, instead of sorting, it's just changing all the numbers in the first colum to "7218" and is not sorting the list
    i dont understand what i am doing wrong, can anyone help?

    Thanks in advance,

    Isaiah

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    when you call sort in your main function, you're creating the prototype. If you're not getting an error telling you "that was bad" I would be very surprised.

    Take another look at the assignements in side the for loops in your sort function.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    In addition, notice that once you call the function correctly, you are still only passing the integers (the numbers in the first column) to sort. You aren't actually sorting the group of information contained on each line. You will probably need a struct or class of some sort to group the number with the temperatures. Then, you can sort each instance of that struct based on its particular number.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    1. x isn't initialized to 0.
    2. The two switch statements are unneccessary. Two if statements checking for 'C' would do the same (and better), since nothing happens anyways when it's 'F'.
    3. What they said about sort(). You're declaring the prototype, not calling the function.
    4. You probably need 2 loops: the first to input, then after that loop call sort(), then after sort() you have a second loop to output.
    5. In sort(), 'hold' should be an int, not a double.
    6. You're missing a closing brace at the end of sort().
    7. What jlou said about needing a structure or something of the sort, to keep the data together in your call to sort().

    I'm not sure about the sort() function itself, it's probably OK but I didn't feel like going through it... I don't know a whole lot about sorting, I prefer to stick stuff in a std::list and then call the STL sort() instead
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM