Thread: Sorting struct

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    5

    Sorting struct

    Hello,

    I am pretty new to all this so bear with me. I need to sort these students based on their test scores.

    My biggest problem is with my sort function. I am not sure of the syntax to call the items the proper way. I also don't know how to call the function properly as I know that i can't pass a int to a struct. Any suggestions would be helpful.

    Also not sure about the z malloc. Just threw that one in there.

    Thank you in advance
    Code:
    struct student{
        int id;
        int score;
    };
    
    
    void sort(struct student* students, int n){
    /*Sort the n students based on their score*/
    
        int idx = 0;
    
        for (int i = 0; i < n; i++)
        { idx = i;
            for (int j = i; j < n; j++)
            {
                if (students[j] < students[idx]){
                    idx = j;
                }
            }
            int min;
            min = students[idx];
            students[idx] = students[i];
            students[i] = min;
        }
    }
    
    
    int main(){
    /*Declare an integer n and assign it a value.*/
        int n = 12;
    
    /*Allocate memory for n students using malloc.*/
        int *m = malloc(sizeof(int)*n);
        int *z = malloc(sizeof(int)*n);
    
    /*Generate random IDs and scores for the n students, using rand().*/
    
        for (int i = 0; i < n; i++)
        {
            m[i] = rand() % 10 + 1;
    
    
        }
        for (int j = 0; j < n; j++)
        {
            z[j] = rand() % 101;
    
        }
    
    
    /*Print the contents of the array of n students.*/
           printf("IDs: ");
        for (int i = 0; i < n; i++)
        {
            printf("%d, ", m[i]);
        }
    printf("\nScores: ");
        for (int j = 0; j < n; j++)
        {
            printf("%d, ", z[j]);
        }
    
    /*Pass this array along with n to the sort() function*/
        sort(z, n);
    
    /*Print the contents of the array of n students.*/
    printf("\n\nSorted:\n\nIDs: ");
        for (int i = 0; i < n; i++)
        {
            printf("%d, ", m[i]);
        }
    printf("\nScores: ");
        for (int j = 0; j < n; j++)
        {
            printf("%d, ", z[j]);
        }
        free(m);
        free(z);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Let's say you have two struct students, x and y. On what basis would you say x was greater than y?

    You need to answer that question unambiguously, since - inside your sort() function - you're obviously expecting the test "students[j] < students[idx]" (where students is an array of struct student) to mean something to a C compiler. It does not.

    Similarly, in the sort() function, what do you expect the assignment "min = students[idx];" (where min is of type int) to do?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    using your logic, what would occur if the student ID a struct is the highest number yet the student has the lowest score. ?

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    You all raise very good points. I've scrapped my sort and z. Back to the drawing board

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Figuring out a working sort algorithm is one issue. Work on that for simple number arrays.
    Then when handling structs you need to flip entire struct records instead of just an int. Or at least flip each struct element.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Before you can sort the structs, you need to use the struct member (score in this case), operator. In C, that is the dot operator: .

    Now your comparisons become like this, to compare (for example), two adjacent students scores: student[i].score > student[i+1].score. Note where the index is placed (before the dot operator), and where the name of the struct member is placed (after the dot operator).

    Please post in plain text, instead of using the colors and other formatting style from your computer. It's hard to read your colors, and it prevents the forum software from adding in line numbering, and other formatting styles it has especially for code.
    Last edited by Adak; 01-18-2014 at 11:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array of struct inside struct
    By blueboyz in forum C Programming
    Replies: 13
    Last Post: 04-24-2012, 02:15 AM
  2. [C] sorting a matrix of struct
    By spaistik in forum C Programming
    Replies: 5
    Last Post: 05-24-2010, 09:18 AM
  3. sorting in a struct within a struct
    By occams razor in forum C Programming
    Replies: 7
    Last Post: 04-15-2007, 02:20 AM
  4. sorting through a struct
    By philipsan in forum C Programming
    Replies: 9
    Last Post: 02-05-2006, 11:55 AM
  5. confused with sorting struct
    By kurz7 in forum C Programming
    Replies: 4
    Last Post: 05-18-2003, 02:46 AM

Tags for this Thread