The golf coach of a local high school team would like to hire you to write a pro
ID: 3812336 • Letter: T
Question
The golf coach of a local high school team would like to hire you to write a program that calculates the average of 5 students on the co-ed golf team after playing three rounds of golf. He would like the program to calculate the average for each student as well as the average for each round. The scores for each student in each round of golf are in the chart below. Also determine the best student average (in golf, the lower scores are better) recorded by the golfers.
Player Round 1 Round 2 Round 3 Ave./Student
Mike Sims78 76 74 76.0
Paula Hill71 78 75 74.7
Steve Jones72 75 73 73.3
Jill Burton73 74 72 73.0
Lee Smith74 7978 77.0
Ave./Round 73.6 76.4 74.4
Explanation / Answer
#include<iostream>
#include <iomanip>
using namespace std;
string* setNames() { /*method to accept input*/
string *names = new string[5]; /*please do not use stack variable, since stack variables may get over written*/
cout<<"Enter 5 names:"<<endl;
for (int i =0; i<5; i++)
cin>>names[i];
return names;
}
int** setScores(string *names) { /*To input the scores. For decoration purpose i have passed the names list*/
int **score = new int*[5];
for (int i=0; i<5; i++) {
score[i] = new int[3];
cout<<"Enter 3 scores for "<<names[i]<<endl;
cin>>score[i][0]>>score[i][1]>>score[i][2];
}
return score;
}
double* calcAvgStu(int **score) { /*finding avg of all players*/
double *avg = new double[5];
for (int i=0; i<5; i++)
avg[i] = (double)(score[i][0]+score[i][1]+score[i][2])/3;
return avg;
}
double* calcAvgRound(int **score) { /*finding avg of all round*/
double *avg = new double[3];
for (int i=0; i<3; i++)
avg[i] = (double)(score[0][i]+score[1][i]+score[2][i]+score[3][i]+score[4][i])/5; //we can use for loop, for optimization we unrolled the loop
return avg;
}
int findSmallest(double *avg) { /*A simple program of finding smallest*/
double smallest = avg[0];
for (int i = 1; i < 5; i++)
if(avg[i] < smallest)
smallest = avg[i];
return smallest;
}
int main() { /*main method*/
string *names = setNames(); /*read names*/
int** score = setScores(names); /*read scores*/
double *avgStu = calcAvgStu(score); /*calc avg scores for each student*/
double *avgRnd = calcAvgRound(score); /*calc avg scores for each round*/
double smallest = findSmallest(avgStu); /*get the smallest avg score*/
cout<<"Player Round1 Round2 Round3"<<endl; /*print header*/
for (int i = 0; i < 5; i++) {
cout<<names[i]<<" "; /*print name and dont go to next line*/
for (int j = 0; j < 3; j++)
cout<<score[i][j]<<" "; /*print scores of theree rounds*/
cout<<fixed<<setprecision(1)<<avgStu[i]<<endl; /*print the avg of ith student*/
}
cout<<"Avr./Round ";
for (int j = 0; j < 3; j++)
cout<<fixed<<setprecision(1)<<avgRnd[j]<<" "; /*print round avg*/
cout<<endl;
cout<<fixed<<setprecision(1)<<smallest<<" was the best recorded student average from the rounds of golf."; /* Finally print the best score!! */
}
/*
Please note the useage of fixed<<setprecision(1) before printing the decimal values
You can also declare fixed<<setprecision(1) once before we perform any outputs
*/
I have commented the code as far as possible. If you face any doubt please feel free to comment below. I shall be glad to help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.