15.9k views
2 votes
• Write a program to find the maximum, minimum, and average score of players. The input to the program is a file containing player name and his score. For maximum and minimum scores, your program needs to output the name of the players along with their scores. Use functions to calculate average, min, and max scores. • The text file will just have players name and scores • nbaStatsFN.txt Sample Input/Output Here’s how the output should look formatted as in first few names: Oladipo 23 Richardson 10 Beal 14 George 16 Lillard 24 James 37 Young 9 Antetokounmpo 25 Bogdanovic 18 McCollum 34 Lowry 11 Prince 28 Hill 17 Middleton 19 Young 22 Westbrook 23 Adams 18 Satoransky 9 Dragic 9 Rozier 33 Hardaway 11

1 Answer

6 votes

Answer:

see explaination

Step-by-step explanation:

#include<iostream>

#include<iomanip>

#include<fstream>

using namespace std;

int getMax(int[],int);

int getMin(int[],int);

double getAvg(int[],int);

const int NUM_PLAYERS = 50;

int main(){

//ARRAYS TO STORE ALL THE INFORMATION OF THE PLAYERS FROM THE FILE

string names[NUM_PLAYERS];

int points[NUM_PLAYERS];

int rebound[NUM_PLAYERS];

int age[NUM_PLAYERS];

int steals[NUM_PLAYERS];

int blocks[NUM_PLAYERS];

int pf[NUM_PLAYERS];

int b, maxIndex, minIndex;

//OPENING THE FILE TO READ THE STATS FROM

ifstream infile;

infile.open("nbastats.txt");

if(!infile){

cout<<"File cannot be opened, please try again.\\";

return -1;

}

string temp;

//STORE THE TABLE HEADER IN TEMP

getline(infile,temp);

int count = 0;

//ITERATE THROUGH THE LINES AS LONG AS WE CAN GET NAMES OF THE PLAYERS

while(infile >> names[count]){

infile>>age[count]>>points[count]>>rebound[count]>>steals[count]>>blocks[count]>>pf[count];

count++;

}

//DISPLAYING THE CONTENTS OF THE FILE IN THE SAME FORMAT AS IN THE FILE

cout<<temp<<endl;

for(int i=0;i<count;i++){

/*

THIS IS THE LINE WHICH DISPLAYS THE OUTPUT IN THE REQUIRED FORMAT

*/

cout<<left<<setw(16)<<names[i]<<setw(8)<<age[i]<<setw(8)<<points[i]<<setw(8)<<rebound[i]<<setw(8)<<steals[i]<<setw(8)<<blocks[i]<<setw(8)<<pf[i]<<endl;

}

//DISPLAYING THE AVERAGE SCORE

cout<<"\\The average score is "<<getAvg(points,count)<<endl;

//DISPLAYING THE AVERAGE STEALS

cout<<"The average steals is "<<getAvg(steals,count)<<endl;

//DISPLAYING THE AVERAGE REBOUND

cout<<"The average rebound is "<<getAvg(rebound,count)<<endl;

//DISPLAYING THE MINIMUM SCORE

minIndex = getMin(points,count);

cout<<"The minimum score by "<<names[minIndex]<<" = "<<points[minIndex]<<endl;

//DISPLAYING THE MAXIMUM SCORE

maxIndex = getMax(points,count);

cout<<"The maximum score by "<<names[maxIndex]<<" = "<<points[maxIndex]<<endl;

return 0;

}

//FUNCTION TO CALCULATE THE AVERAGE SCORE GIVEN THE ARRAY OF SCORES

double getAvg(int points[],int size){

double sum = 0;

for(int i=0;i<size;i++){

sum = sum+points[i];

}

return double(sum/size);

}

//FUNCTION TO RETURN THE INDEX OF PLAYER WITH MINIMUM SCORE

int getMin(int points[], int size){

int minIndex,minScore;

minScore = points[0];

minIndex = 0;

for(int i=1;i<size;i++){

if(points[i] < minScore){

minScore = points[i];

minIndex = i;

}

}

return minIndex;

}

//FUNCTION TO RETURN THE INDEX OF PLAYER WITH MAXIMUM SCORE

int getMax(int points[], int size){

int maxIndex,maxScore;

maxScore = points[0];

maxIndex = 0;

for(int i=1;i<size;i++){

if(points[i] > maxScore){

maxScore = points[i];

maxIndex = i;

}

}

return maxIndex;

}

User Rafael Fragoso
by
4.5k points