In c++ programing , complete this assigment. The instructions are given in the f
ID: 3701356 • Letter: I
Question
In c++ programing , complete this assigment. The instructions are given in the first picture, I will also post the code that needs to be edited , along with the text file needed to complete the program. Please follow all insturctions in first picture, thank you.
Instructions :
Code that needs to be edited :
#include <iostream>
#include <fstream>
using namespace std;
const int NUM_PLAYERS = 25;
const int MAX_COLS = 6; // Number of categories in players statistics
void get_average(int [][MAX_COLS], int, int [], int );
void get_min_index(int [][MAX_COLS], int, int [], int );
int main()
{
int pscores[NUM_PLAYERS][MAX_COLS];
int avg[MAX_COLS]; // Array passed as argument to get_average. Avg values are stored in this array
int minim[MAX_COLS]; // Array passed as argument to get_min_index. Avg values are stored in this array
string pname[NUM_PLAYERS], temp;
int i, num, score, min_index;
int row, col;
ifstream infile;
infile.open("nbastats.txt");
if (!infile){
cout << "Invalid file name ";
return -1;
}
// Skip the header line
getline(infile, temp);
// Read the rest of the data and store them in the arrays
for (row = 0; row < NUM_PLAYERS; row++){ // ROW BY ROW
infile >> pname[row]; // First read the player name
// Now read the stats for the player read above
for (col = 0; col < MAX_COLS; col++){
infile >> pscores[row][col];
}
}
// call the functions to calculate average and min for each category
get_average(pscores, NUM_PLAYERS, avg, MAX_COLS );
get_min_index(pscores, NUM_PLAYERS, minim, MAX_COLS );
// Write the code to display the result
}
void get_average(int scores[][MAX_COLS], int s_size, int avg[], int a_size )
{
// This function should calculate the average of each category of players statistics
// Store the results in avg[]
// s_size contains the number of rows of scores[][]
// a_size is the size of avg[]
//write your code below
}
void get_min_index(int scores[][MAX_COLS], int s_size, int minimum[], int m_size )
{
// This function should find the index of the minimum value in each category of players statistics
// Store the results in minimum[]
// s_size contains the number of rows of scores[][]
// m_size is the size of avg[]
// Write you code below
}
--------------------------------------------------------------------------------------------------------------------------------
nba.txt text file that may be needed (not 100% needed):
Name age Points Rebound Steals Blocks PF
Oladipo 25 23 2 3 0 1
Richardson 24 10 3 2 0 0
Beal 24 14 3 2 0 2
George 27 16 2 1 1 2
Lillard 27 24 7 3 0 1
James 33 37 10 1 0 3
Young 32 9 3 1 0 3
Antetokounmpo 23 25 10 1 0 3
Bogdanovic 28 18 8 0 0 1
McCollum 26 34 4 1 0 2
Lowry 32 11 6 1 0 3
Prince 24 28 6 2 0 0
Hill 31 17 4 2 1 2
Middleton 26 19 7 2 1 2
Young 29 22 9 5 3 5
Westbrook 29 23 8 4 0 6
Adams 24 18 10 0 2 2
Satoransky 26 9 4 3 1 4
Dragic 31 9 4 2 0 4
Rozier 24 33 5 5 0 1
Hardaway 26 11 4 3 0 1
Cook 25 17 4 1 1 1
Williams 31 26 2 1 1 1
Rivers 25 11 1 4 0 2
Bogdanovic 25 4 1 3 0 2
Explanation / Answer
void get_average(int scores[][MAX_COLS], int s_size, int avg[], int a_size )
{
int sum[a_size]; // initializing array to store the sum
//initializing sum of each column as 0
for(int i = 0; i< a_size ; i++)
{
sum[i] = 0;
}
for (int i=0 ; i< s_size ; i++)
{
for (int j=0; j< a_size; j++)
{ //get the sum of each row in a column
sum[j] = sum[j] + scores[i][j];
}
}
//divide the sum of elements of each row in a column with number of rows to get average
for (int i = 0; i< a_size ; i++)
avg[i] = sum[i]/s_size;
}
void get_min_index(int scores[][MAX_COLS], int s_size, int minimum[], int m_size )
{
int min[a_size]; // array to store minimum values of each column
for(int i= 0; i< a_size; i++)
{
min[i] = scores[0][i]; //set the minimum value as the first element of each column
minimum[i] = 0; //set the minimum index as zero
}
for (int i=0 ; i< s_size ; i++)
{
for (int j=0; j< a_size; j++)
{
if( scores[i][j] < min[j]) //if the minimum value stored for colum j is less than the current value in column j
{
min[j] = scores[i][j]; // set the minimum value
minimum[j] = i; // set the idex of the minimum value as row number
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.