Thread: Sorting student records by letter grade

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    Sorting student records by letter grade

    I am sorting a file containging student records by grade letter, highest to lowest. I can sort it but my question is how do I make say a B+ be above a B in the order? This is my code:

    insert
    Code:
    void sortListGrade(STUDENT records[], int count)
    {
       STUDENT tempData;
       int smallest;
       int current;
       int walk;
        
         for (current = 0; current < count - 1; current++)
         {
    	      smallest = current;
              for (walk = current + 1; walk <= count - 1; walk++)
    		  {
    	     
                 if ((strcmp(records[walk].grade, records[smallest].grade)) < 0)
    		         smallest = walk;
    		  }
              tempData = records[current];
              records[current] = records[smallest];
              records[smallest] = tempData;
         }
    	 
       return;
    }
    right now its printing
    A
    A
    B
    B+
    B-
    B-
    ...

    I need it to print
    A
    A
    B+
    B
    B-
    B-
    ...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll need to modify how you compare. Don't use strcmp, use something else instead.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could keep strcmp() if you made some other changes. Already, B+ sorts out as less than B-, so that part is done. (The B+ comes higher on the list than B-)

    The only problem is the plain grades, without a plus or minus sign. Change your logic so plain grades become a letter and ASCII 44 (a comma). Now the A+'s sort out as less than A's, and the A's sort out before the A-'s.

    You'll need to add the if statement, so if a grade[1], is a comma, only the letter grade will be printed, and not the comma.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 03-03-2010, 01:33 AM
  2. Invalid Output of Minimum and Maximum Value in Array
    By georgio777 in forum C Programming
    Replies: 10
    Last Post: 09-19-2009, 03:17 AM
  3. reading a file into a block
    By mickey0 in forum C++ Programming
    Replies: 19
    Last Post: 05-03-2008, 05:53 AM
  4. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM