Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions