Click here to Skip to main content
15,921,530 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Imagine that I need to track record of students for 24 months time, for 3 schools. I created a structure like this:

const int constMonths[24];
struct record{
    int students[24];

struct record school[3];

Say that I want to sort which school has the highest students only for the first 12 months, and display it to something like this:

School 1: 4992
School 3: 3029
School 2: 1633

I've searched for several sorting methods like bubble sort, QuickSort and exchange sort, but seems like I couldn't get the logic right when it comes to the sorting schools thing.

How can this be done? Thank you.


This is what I've tried so far:

#include <stdio.h>

int main()
    int newArray[4];
    int testArray[4];
    int q2[2] = {-1, -1};
    int i;
    newArray[0] = 488;
    newArray[1] = 61;
    newArray[2] = 3884;
    newArray[3] = 141;
    newArray[4] = 20;
    testArray[0] = newArray[0];

    for (i = 0; i < 4; i++)
        if (q2[0] = -1)
             q2[0] = i;
        else if (newArray[i] >= newArray[q2[0]])
             q2[1] = q2[0];
             q2[0] = i;
        else if (newArray[i] >= newArray[q2[1]])
             q2[1] = i;
    return 0;

The result if we access newArray[q2[i]]:

for (i =0, i<2; i++)
    printf("School %d: %d", q2[i], newArray[q2[i]]);

School #3: 3884
School #1: 488

Any way to display more than 2 of them? thank you.

Please, I couldn't think of the logic behind this, perhaps someone can help me.
Updated 7-Aug-14 0:29am
farhad bat 7-Aug-14 6:21am    
constMonths is int array and you write:
int students[constMonths];
you should put integer number between [ , ] .why fill it by constMonths?
farizluqman 7-Aug-14 6:26am    

1 solution

All you need to do (and since this is your homework you will get no code) is use one of the sorting algorithms you mention - and explanations / code for all of those is easily available to anyone with access to Google - and provide a function to compare two structs according to your criteria (which you don't explain). Which is trivial, pretty much, regardless of the criteria.

So what is your problem? We aren't going to do it for you: it's your homework and your grade after all. But it's a pretty trivial problem if you just sit down and think about it for a couple of minutes.

Try it yourself: you may find it's a lot easier than you think!
Share this answer
farizluqman 7-Aug-14 6:26am    
please see the update I've done
OriginalGriff 7-Aug-14 6:45am    
Not your homework? Oh, right. I'm sure there are loads of companies who pay people to sort trivial data...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900