Sorting Problems

This is a discussion on Sorting Problems within the C Programming forums, part of the General Programming Boards category; Ok I dont know what I am doing wrong, I read through about 30 threads on here and tried googling ...

  1. #1
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16

    Sorting Problems

    Ok I dont know what I am doing wrong, I read through about 30 threads on here and tried googling for something that might help me out.

    I found one with a problem pretty close to mine and I tried out using what worked for that person (strcmp) but that makes the output even worse then what it is for what i have now.

    If I comment out this function the program works ok, it calculates the summary fine, prints everything out fine but as soon as I compile and run with this in it ... I get some strange outputs and only the first few records are in order. I am really lost as to what I have done wrong here.

    I know there are tons of questions already posted about bubble sorting and I apologize for having to ask yet another ;p


    Code:
    void SortArrs ()
    
    {
         for(i=0; i<RECORDCOUNT-1; i++)
         for(j=0; j<RECORDCOUNT-1; j++)
                   if (FNAME[i]>FNAME[j])
                                 {
                                          
                                            strcpy(temp, FNAME[i]); 
                                            strcpy(FNAME[i], FNAME[j]);       
                                            strcpy(FNAME[j], temp);
                                                                                    
                                            strcpy(temp, LNAMEARR[i]);        
                                            strcpy(LNAMEARR[i], LNAMEARR[j]);
                                            strcpy(LNAMEARR[j], temp);
                                            
                                            strcpy(temp, CLASSARR[i]);
                                            strcpy(CLASSARR[i], CLASSARR[j]);
                                            strcpy(CLASSARR[j], temp);
                                            
                                            temp[15] = CHARGEARR[i];
                                            CHARGEARR[i]=CHARGEARR[j];
                                            CHARGEARR[j]= temp[15];
                                            
                                            temp[15]= GSTARR[i];
                                            GSTARR[i]=GSTARR[j];
                                            GSTARR[j]= temp[15];
                                            
                                            temp[15]= PSTARR[i];
                                            PSTARR[i]=PSTARR[j];
                                            PSTARR[j]= temp[15]; 
    
                                            temp[15]= TOTALARR[i];
                                            TOTALARR[i]=TOTALARR[j];
                                            TOTALARR[j]= temp[15];
                                   }
                  }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    To me it looks like each record should be a struct. If so, then I'd swap using structure assignment. And then only the difference from sorting ints would be the comparison.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    We havent covered structures at all in my course, I asked the teacher about them the other day and he said not to worry about them as we arent going to cover them at all :/

    The Problem we were given asks us to use seperate parallel arrays for all the information we read in from a file and then sort them in alphabetical order in "last name, first name" format.

    I have everything working but this bubble sort and its giving me a real headache.

    Hey I need to start paying you for answering all my questions ;p

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >The Problem we were given asks us to use seperate parallel arrays for all the information

    Could you post the variable definitions?

    >Hey I need to start paying you for answering all my questions ;p

    I've got no problem with that.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    Code:
    int  FCOUNT, BCOUNT, ECOUNT, RECORDCOUNT, TOT500, ERROR, NUMBER, i, j, PCOUNT;
    
    char NAME[20], LNAME[20], *CLASS, CODE, BUFFER, 
    *REASON, FNAME[100][25], LNAMEARR[100][25], CLASSARR[100][15], temp[15]; 
    
    float CHARGE, CHARGEARR[100],GST,GSTARR[100], PST, 
    PSTARR[100], TOTAL, TOTALARR[100], TOTTOTAL, AVGTOT, HOURS;
    No doubt you wouldnt lol

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You ought to make temp big enough to swap FNAME and LNAMEARR. You need to use a temporary of type float to swap floats, rather than trying with a single char (that is one beyond its array bounds). And use strcmp to compare strings.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    ok that fixed my one problem but it still isnt sorting right. The output is nowhere near sorted alphabetically. It also prints out one record of blank information (all zeros and just " , " for the name). Maybe I am doing something wrong with strcmp? As far as I can tell I have done the same as pretty much every instance I have seen it used in. :/



    Code:
    void SortArrs ()
    
    {
         for(i=0; i<RECORDCOUNT-1; i++)
         for(j=0; j<RECORDCOUNT-1; j++)
                   if (strcmp(FNAME[i], FNAME[j])>0)
                                 {
                                          
                                            strcpy(temp, FNAME[i]); 
                                            strcpy(FNAME[i], FNAME[j]);       
                                            strcpy(FNAME[j], temp);
                                                                                    
                                            strcpy(temp, LNAMEARR[i]);        
                                            strcpy(LNAMEARR[i], LNAMEARR[j]);
                                            strcpy(LNAMEARR[j], temp);
                                            
                                            strcpy(temp, CLASSARR[i]);
                                            strcpy(CLASSARR[i], CLASSARR[j]);
                                            strcpy(CLASSARR[j], temp);
                                            
                                            tempf[15] = CHARGEARR[i];
                                            CHARGEARR[i]=CHARGEARR[j];
                                            CHARGEARR[j]= tempf[15];
                                            
                                            tempf[15]= GSTARR[i];
                                            GSTARR[i]=GSTARR[j];
                                            GSTARR[j]= tempf[15];
                                            
                                            tempf[15]= PSTARR[i];
                                            PSTARR[i]=PSTARR[j];
                                            PSTARR[j]= tempf[15]; 
    
                                            tempf[15]= TOTALARR[i];
                                            TOTALARR[i]=TOTALARR[j];
                                            TOTALARR[j]= tempf[15];
                                   }
                  }

  8. #8
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    I figured it out finally. I was sorting it by firstname instead of lastname for some reason and thats why it looked wrong. There were a few other problems so thanks for your help

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    tempf[15]
    You only need one.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    Is that because tempf is a float array so it just is big enough to fit in the floats that are going into it? ... Sorry I like to know the reasons behind doing things like that.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       float a = 10.0F, b = 20.0F, swap;
       printf("a = %g, b= %g\n", a, b);
       swap = a;
       a = b;
       b = swap;
       printf("a = %g, b= %g\n", a, b);
       return 0;
    }
    
    /* my output
    a = 10, b= 20
    a = 20, b= 10
    */
    An array, say
    Code:
    float a[15];
    may contains the elements a[0] ... a[14]. An a[15] is off the end of the array -- attempting to use it is undefined behavior.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problems.
    By Sedvan in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2008, 01:13 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  5. Sorting structures
    By RedRum in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 01:19 PM

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