C++ programming using visual studio to program do program which calculates and d
ID: 3858452 • Letter: C
Question
C++ programming using visual studio to program
do program which calculates and displays a baseball player's batting average and proficiency. There are an unknown number of players.
For any game in which a player participates, his batting average is calculated by dividing his number of hits by his number of at-bats. So if a player had 1 hit in 3 at-bats (we say he was "1 for 3"), his batting average would be .333. If a player was 3 for 4, his batting average for the game would be .750. And if a player played in ten games in which he was 9 for 36, his batting average for the ten games would be .250 (batting averages are always expressed in 3 decimals).
In main, repeatedly have the user enter a player's full name and have the program call the two functions described below, until all players' are processed.
The program will have two functions:
1) this function will receive a player's name into its parameter list; use the name to prompt the user to enter the player's hits and at-bats for each of his games, for any number of games. Each player processed by this function may have played in a different number of games. Validate that for each game, the hits and at-bats are zero or greater, and the hits must not exceed the at-bats (hits and at_bats are data type "int"). Summarize the total hits and at-bats for all games and calculate the batting average for the player, and return it to main.
2) in main the call to this function will pass it a player's batting average; the function will then determine the player's batting proficiency based on the chart below. The function will then return the batting proficiency to main.
Batting Average Range Proficiency
.000 less than 250 Minor Leaguer
.250 less than .300 All Star.
300 less than .400 Hall of Fame
over .400 ---------------- King of Baseball
In main the program will display the player's name, batting average, and proficiency in a user-friendly manner. Display the batting averages to 3 decimal points (don't worry about the leading zero).
Provide 2 screen prints by entering the following data for two players (name; hits & at-bats):
-Will E. Maize; 1 for 3, 2 for 4, 0 for 0, 2 for 3, 1 for 4, 3 for 5
-Jo Dim Magico; 1 for 4, 2 for 3, 3 for 4
Explanation / Answer
// Please comment and upvote the answer,if u find it helpful...Thanx.
#include <iostream>
#include<stdlib.h>
using namespace std;
float calculateAverage(char *name){
int hits,at_bats,number,hits_total=0,at_bats_total = 0;
float avg;
cout<<" Enter the number of games played by "<< name<< " : ";
cin >> number;
for(int i = 0;i<number;i++)
{
cout << " Enter Game "<< i+1<< " hits : ";
cin >> hits;
cout << " Enter Game "<< i+1<< " at_bats : ";
cin >> at_bats;
if (hits > at_bats || hits == 0 || at_bats == 0){
cout << " **** Please enter data correctly **** ";
cout << " 1) hits or at_bats should be greater than zero ";
cout << " 2) at_bats > hits ";
i--;
}
hits_total = hits + hits_total;
at_bats_total = at_bats_total + at_bats;
}
avg = (float(hits_total))/ (at_bats_total);
return avg;
}
char *calculateProfiency(float averag){
float average = averag;
char *pro = (char*)malloc(30);
if (average < 0.250 )
{
pro = "Minor League";
}
else if (average < 0.300 )
{
pro = "All Star ";
}
else if (average < 0.400 )
{
pro = "Hall of Fame";
}
else if (average > 0.250 )
{
pro = "King of BasketBall";
}
return pro;
}
int main()
{
char name[100],*prof;
float averag;
char c;
while(1){
cout << "Enter Player name : ";
cin >> name;
averag = calculateAverage(name);
prof = calculateProfiency(averag);
cout<<" Name : "<<name<<" "<<"Average : "<< averag<<" "<<"Proficiency : "<< prof<<endl;
cout<<" Enter any key to continue";
cout<< " Enter 'n' or 'N' to exit ";
cin >> c;
if(( c == 'n') || (c == 'N'))
{
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.