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

Lab Assignment #6 Relevant Programming Concepts: Two-Dimensional Arrays Function

ID: 3835570 • Letter: L

Question

Lab Assignment #6 Relevant Programming Concepts: Two-Dimensional Arrays Functions Problem 1: Six players compete for a single position on the national team. The coach has obtained their statistics on the number of per match for their last seven matches. These are stored in a file points called stats.txt. Every row corresponds to the stats ofa single player 10 12 13 7 9 8 11 16 17 5 9 16 14 12 14 16 12 12 16 12 14 22 24 21 18 16 12 10 19 17 16 18 20 21 22 15 18 13 19 9 22 24 Write a C program that a) Reads the values of stats.txt into a two-dimensional array of size 6x7 using the following function prototype b) Finds void read ar (FILE *in, int x I 1171): the player with the highest average number of points per match. It displays the player number (ie corresponding row), and his average number of points per match. points. amber and average for the match with the highest average number of

Explanation / Answer

#include <stdio.h>

void DisplayHighestAverageMatch(int x[][7])

{
   double matches[7] = {0};
   double max_avg = 0;
   int max_match= 0;
   for (int i=0; i<7; i++)
   {   for(int j=0; j<6;j++)
   {
       matches[i] += x[j][i];
   }
   matches[i] /= 6;
   if ( matches[i] > max_avg )
   {
       max_avg = matches[i];
       max_match = i+1;
   }
   }
   printf(" Match %d has the highest average of points: %f", max_match , max_avg);
}

void DisplayHighestAveragePlayer(int x[][7])
{
   double players[6] = {0};
   double max_avg = 0;
   int max_player = 0;
   for (int i=0; i<6; i++)
   {   for(int j=0; j<7;j++)
   {
       players[i] += x[i][j];
   }
   players[i] /= 7;
   if ( players[i] > max_avg )
   {
       max_avg = players[i];
       max_player = i+1;
   }
   }
   printf(" Player %d has the max average per match : %f", max_player , max_avg);
}

void read_ar(FILE* in, int x[][7])
{
   if (in == NULL )
   {
       printf("Error! Could not open file ");
       return;
   }
   int num;

   for (int i=0; i<6; i++)
   {   for(int j=0; j<7;j++)
   {
       fscanf(in, "%d", &x[i][j]);
   }
   }
   DisplayHighestAveragePlayer(x);
   DisplayHighestAverageMatch(x);
}