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.
UPDATE:
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.