3. Write a C++ program that will input the number of wins and losses that a base
ID: 3685595 • Letter: 3
Question
3. Write a C++ program that will input the number of wins and losses that a baseball team acquired during a complete season. The wins should be input in a parameter-less value returning function that returns the wins to the main function. A similar function should do the same thing for the losses. A third value returning function calculates the percentage of wins. It receives the wins and losses as parameters and returns the percentage (float) to the main program which then prints the result. The percentage should be printed as a percent to two decimal places. Sample Run: Please input the number of wins 80 Please input the number of losses 40 The percentage of wins is 66.67%
i cant figure out how to get the percentage to two decimal places i have everything else done
Explanation / Answer
Solution.cpp
#include <iostream>
#include <iomanip>
using namespace std;//links std namespace to the compiles
int winsfunction(int);//function declarations
int lossesfunction(int);
float percentagefunction(int,int);
int winsfunction(int noofwins)
{//function definition
cout<<"no of wins :"<<noofwins<<endl;
return noofwins;
}
int lossesfunction(int nooflosses)
{
cout<<"nooflosses :"<<nooflosses<<endl;
return nooflosses;
}
float percentagefunction(int noofwins,int nooflosses)
{
int totalmatches=noofwins+nooflosses;
float percentage=((float)noofwins/(float)totalmatches)*100;
cout<<"The percentage of wins is :"<< setprecision(4)<<percentage<<endl;//calcu;ates win percentages
return percentage;
}
int main()
{//main function
int wins;
int losses;
cout<<"please enter no of wins :";
cin>>wins;
cout<<"please enter no of losses :";
cin >> losses;
winsfunction(wins);//callinf function
lossesfunction(losses);
percentagefunction(wins,losses);
return 0;
}
output
please enter no of wins :80
please enter no of losses :40
no of wins :80
nooflosses :40
The percentage of wins is :66.67
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.