Question: Help returning values in arrays? (C programming) This is the input: 10 14 A 447 252 68 34 978 2 C 230 299 597 180

Help returning values in arrays? (C programming)

This is the input:

10 14 A 447 252 68 34 978 2 C 230 299 597 180 9 27 A 318 220 97 28 1317 32 C 563 450 547 112 28 8 C 669 260 200 36 171 11 S 179 45 1342 732 174 19 S 74 249 861 1165 6 21 A 757 240 97 119 2032 15 S 275 177 588 577 52 6 C 886 401 327 109 48

This is my code:

#include #include

#define MAX 100 #define SIZE 5

float getRanking(char ch, int points[]); int findMax(char key, char category[], int id[], float scores[], int n);

int main() {

int id[MAX]; char category[MAX]; int n, i, j; int combat_score[SIZE]; float scores[MAX]; int m;

FILE *ifp = fopen("sample.txt", "r");

//Read in number of sets fscanf(ifp,"%d", &n);

//Read ID and category for (i=0; i fscanf(ifp, "%d %c", &id[i], &category[i]);

//Read 5 combat_scores for (j=0; j fscanf(ifp, "%d", &combat_score[j]);

}

scores[i] = getRanking(category[i], combat_score);

}

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

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

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

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

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

fclose(ifp);

return 0; }

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

int i,j, loc=0; float first=0;

for (i=0; i

if (key == category[i]) {

if (first < scores[i]) { first = scores[i]; loc = i; }

}

} //Removing max. So the next time we can get second max //Making max to minimum scores[loc] = 0;

return loc;

}

//Pre-Condition: The category has to be C, S, or A //Post-Condition: The float value of "sum" will be returned in // correspondence to the category letter float getRanking(char ch, int points[]) {

float sum = 0;

if (ch == 'C') sum += (points[0] * 5 + points[1] * 5 + points[2] + points[3] + points[4] * 2) / 10.0; else if (ch == 'S') sum += (points[0] + points[1] + points[2] * 5 + points[3] * 5 + points[4] * 2) / 10.0; else if (ch == 'A') sum += (points[0] + points[1] * 2 + points[2] * 2 + points[3] + points[4] * 5) / 10.0;

return sum;

}

This is my output, it returns the correct id# but all scores are 0:

A: 21 0.00 C: 6 0.00 C: 32 0.00 S: 11 0.00 S: 19 0.00

This is my output if I remove "scores[loc] =0;" :

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

This is the correct ouput:

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

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!