Thread: Sorting within a stucture

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Brooklyn, NY
    Posts
    21

    Sorting within a stucture

    Code:
    struct stud{
    
              char name[30];
              int grade[4];
              float avg;
    
    }
    Then in the program i do this:

    Code:
    struct stud students[];
    
    ..
    ..
    Then I need a function which will sort the grades for each student from lowest to highest..
    N is the number of students
    Code:
    void sort(stuct student[], int n){
    
            int i, j, lowest, highest, temp;
    
            for(i=0;i<n;i++)
                 for(j=0;j<4;j++){
                      lowest=student[i].grade[j];
                      highest=student[i].grade[j+1];
    
                      if(lowest>highest){
                     
                         temp=student[i].grade[j];
                         student[i].grade[j]=student[i].grade[j+1];
                         student[i].grade[j+1]=temp;
                 
                        }
                 }
    will this work ??
    any help?

    all I want to know is how to sort the 4 grades within the structure for each student.
    Last edited by Zeff; 11-28-2006 at 03:48 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Here you go... look at these: http://eternallyconfuzzled.com/tuts/sorting.html

    Specifically Bubble Sort or Selection Sort as those will probably be the easiest for you to understand conceptually. You're going to want to use one of those to sort the grades, and then you're going to need another loop around your sort to go through each student.
    Sent from my iPad®

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you make your temp to be
    struct stud temp;

    And swap the whole structure, then you could be onto something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Except that the OP is attempting to sort an array within the struct. . . The OP appears to be sorting the grades of each student, not the students themselves.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    Brooklyn, NY
    Posts
    21
    Yeah, I do not want to sort the student themselves(yet), just the 4 grades within each student

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    Brooklyn, NY
    Posts
    21
    I was also working it like this:

    Code:
    for(i=0;i<n;i++) // going through each student
    
        for(j=0;j<4;j++) // going through each grade within the structure (4 grades)
    
           for(k=j+1;k<5,k++) // checking each grade to all the other grades
    
              if(students[i].grade[j]>students[i].grade[k]){
                 temp=students[i].grade[j];
                 students[i].grade[j]=students[i].grade[k];
                 students[i].grade[k]= temp;
                }

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Salem
    Well if you make your temp to be
    struct stud temp;

    And swap the whole structure, then you could be onto something.
    Hehe... I said the same thing at first but then deleted my post. I misunderstood what he wanted to sort because I didn't bother reading the last line.
    Sent from my iPad®

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    Brooklyn, NY
    Posts
    21
    Quote Originally Posted by Zeff
    I was also working it like this:

    Code:
    for(i=0;i<n;i++) // going through each student
    
        for(j=0;j<4;j++) // going through each grade within the structure (4 grades)
    
           for(k=j+1;k<5,k++) // checking each grade to all the other grades
    
              if(students[i].grade[j]>students[i].grade[k]){
                 temp=students[i].grade[j];
                 students[i].grade[j]=students[i].grade[k];
                 students[i].grade[k]= temp;
                }
    Is this any good ?

    I'm pretty sure it will work, but I think that there is an easier way.

    What do you guys think?

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well there different ways of sorting. But that above sorting method looks ok.

    ssharish2005

  10. #10
    Registered User
    Join Date
    Sep 2006
    Location
    Brooklyn, NY
    Posts
    21
    Thanks.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I prefer to build functions for such purposes... You trying to sort an array - so sort function need not to know that the array is a memeber of some struct or not

    I think using such function makes code more readable - both inside the sort loop, and outside - where we scanning all students...
    I mean smthing like
    Code:
    sortGrades(int grades[], int size);
    
    for(i=0;i<n;i++) 
    {
       sortGrades(students[i].grade,4);
    }
    Last edited by vart; 11-28-2006 at 11:58 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    PS. k<5 - goes out of bounds of the array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM