Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with finding values in arrays. Where did I go wrong? (C Prog.) This is

ID: 3802924 • Letter: N

Question

Need help with finding values in arrays. Where did I go wrong? (C Prog.)

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 what the output should be:

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

This is my code:

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<n; i++) {
        fscanf(ifp, "%d %c", &id[i], &category[i]);

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

            }

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

        }

        //insertionSort(category, scores, id, size);
        //sort(category, scores, id, n);


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

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

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

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

         m = findMaxIndex('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, loc= 0;
    float first=0;
    float second=0;
    float sec=0;

    for (i=0; i<n; i++) {
       sec = second;


        if (key == category[i]) {
           first = scores[i];
           second = scores[i];

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

            }

            else if (sec < scores[i]) {
            second=scores[i];
            loc = i;
            sec=second;

            }

        }

    }

    return loc;

}

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:

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

Explanation / Answer

/*
This is the input in sample.txt:
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 what the output should be:
A: 21 1171.00
C: 6 696.70
C: 32 578.00
S: 11 1094.20
S: 19 1046.50
*/

#include <iostream>
#include <vector>
//#include <fstream>
//#include <cstdio>

#define SIZE 5   // As per the loop
#define MAX 50 // Assuming, as it is not given in question
using namespace std;


float getRanking(char ch, int points[]);
int findMax(char key, char category[], int id[], float scores[], int n, int c);
// In findMax c keeps count of how many times each category alphabet was called before
void sort(char category[], float scores[], int id[], int n);

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

   //Input file is read
   FILE *ifp = fopen("sample.txt", "r");
//Read in number of sets
fscanf(ifp,"%d", &n);
//Read ID and category
for (i=0; i<n; i++) {
fscanf(ifp, "%d %c", &id[i], &category[i]);
//Read 5 combat_scores
for (j=0; j<SIZE; j++) {
fscanf(ifp, "%d", &combat_score[j]);
}
scores[i] = getRanking(category[i], combat_score);
      
}

       sort(category, scores, id, n);

       //The last value in findMax indicates how many times the same key category has been passed in findMax before.
       //If it is one, it means get the max value for which category matches key.
       //If it is 2, get the second max value for which category matches key.
       //And so on..

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

fclose(ifp);

   return 0;
}

void sort(char category[], float scores[], int id[], int n)
{
   int i,j;
   bool first_hit = 0;
   float t_score;
   char t_category;
   int t_id;      

       for (i = 1 ; i <= n-1 ; i++) {      
       j = i;      
      
       while ( category[j] == category[j-1] && j > 0 && scores[j] < scores[j-1] ) {
           t_score = scores[j];
           scores[j]= scores[j-1];
           scores[j-1] = t_score;

           t_id = id[j];
           id[j] = id[j-1];
           id[j-1] = t_id;

           t_category = category[j];
           category[j] = category[j-1];
           category[j-1] = t_category;

           j--;
       }  
      

       }
       //Display the sorted array. The values are sorted in such a way that :
       //For every category the scores values are in ascending order.
       //Note that the categories themselves are not sorted.

       for (i = 0 ; i < n ; i++)
           printf("%c: %d %.2f ",category[i],id[i],scores[i]);

}

  
int findMax(char key, char category[], int id[], float scores[], int n, int c) {
int i,j;
   int loc_category[MAX]; // aray that stores the locations corresponding to key category

   for (i=0, j=0; i<n; i++){
if (key == category[i])           
{
           loc_category[j++] = i;
           cout<< loc_category[j-1] << " ";
   }
   }

return loc_category[j-c];
}

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;
}