Question: How can I return the second highest value in an array? (C Programming) This is the input. Column one is the category, two is the

How can I return the second highest value in an array? (C Programming)

This is the input. Column one is the "category", two is the "id", and three is the "scores"

for example, if I have this input:

A 21 1171.00 S 19 1046.50 S 11 1094.20 S 15 638.10 C 6 696.70 A 27 756.50 C 8 522.30 C 32 578.00 A 14 601.10 C 2 344.00

my output needs to look like:

A: 21 1171.00 C: 6 696.70 C: 32 578.00 S: 11 1094.20 S: 19 1046.50

This is a sample of what I have:

int findMax (char key, char category[], float scores[], int n);

int main(){

m = findMax('A', category, scores, n); printf("%c: %d %.2f ",category[m],id[m],scores[m]);

m = findMax('C', category, scores, n); printf("%c: %d %.2f ",category[m],id[m],scores[m]);

m = findMax('C', category, scores, n); printf("%c: %d %.2f ",category[m],id[m],scores[m]);

m = findMax('S', category, scores, n); printf("%c: %d %.2f ",category[m],id[m],scores[m]);

m = findMax('S', category, scores, n); printf("%c: %d %.2f ",category[m],id[m],scores[m]);

}

int findMax(char key, char category[], float scores[], int n) {

int i, max=0, loc;

for (i=0; i if (key == category[i]){ if (scores[i] > max){ max = scores[i]; loc = i; } }

}

return loc;

}

My codes output returns this:

A: 21 1171.00 C: 6 696.70 C: 6 696.70 S: 11 1094.20 S: 11 1094.20

How can I return the second highest value for the 'C' and 'S' category?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!