Getting twelve strikes in a row results in the maximum attainable score of 300. In this case, twelve rolls are made. The least number of rolls possible is eleven and the greatest is twenty-one. In fact there is a correlation between the number of rolls in a game and the game score. Many rolls correspond to low scores and few rolls to high scores. The better a bowler you are, the fewer rolls you get!
The problem is to print the total score for each of a series of games, as well as the minimum, average, and maximum scores. Assume the input is given as a series of roll scores for each game and includes at least one game.
Solution:
#include
<iostream>
using
namespace std;
int
gamecalc();
//calculates gamescore for the current game
void
framecalc(int *pgamescore,int *pframe,int *prollscore,int *pr1);
//calculates the framescore for the current game,adds it to gamescore
//increases frame by 1 and updates rollscore and r1
void
update(int gamescore,int *pnumber,int *pmin,int *pmax,int *ptotal);
//prints gamescore,and updates number,min,max and total to reflect gamescore
void
main()
{
int gamescore;//score for the current game
int number=0;//current number of games played
int max=0,min=300;//current maximum and minimum game score
int total=0;//accumulated total of all game scores
float average;//average of all game scores
int newgame=1;
while(newgame){
gamescore=gamecalc();
update(gamescore,&number,&min,&max,&total);
cout<<"If you want to bowl another game,enter a 1."<<endl;
cout<<"If you wish to stop,enter a 0."<<endl;
cin>>newgame;
}
average=((float)total)/number;
cout<<"Minimum game score "<<min<<endl;
cout<<"Average game score "<<average<<endl;
cout<<"Maximum game score "<<max<<endl;
cout<<"Number of games "<<number<<endl;
}
int
gamecalc()
{
int gamescore=0;//score for the current game
int frame=1;//the current frame
int rollscore;//first roll score of the current frame
int r1;//succeeding roll score after rollscore
cout<<"Please enter the number of pins down on this roll"<<endl;
cin>>rollscore;
cout<<"Please enter the number of pins down on this roll"<<endl;
cin>>r1;
while(frame<=10){
framecalc(&gamescore,&frame,&rollscore,&r1);
}
return gamescore;
}
void
framecalc(int *pgamescore,int *pframe,int *prollscore,int *pr1)
{
int framescore;//score for current frame
int r2;//rollscore after r1
if(*prollscore==10){
cout<<"Please enter the number of pins down on this roll"<<endl;
cin>>r2;
framescore=10 + *pr1 + r2;
*prollscore=*pr1;
*pr1=r2;
}
else if((*prollscore + *pr1)==10){
cout<<"Please enter the number of pins down on this roll"<<endl;
cin>>r2;
framescore=10+r2;
*prollscore=r2;
if(*pframe<10){
cout<<"Please enter the number of pins down on this roll"<<endl;
cin>>(*pr1);
}
}
else{
framescore=*prollscore + *pr1;
if(*pframe < 10){
cout<<"Please enter the number of pins down on this roll"<<endl;
cin>>(*prollscore);
cout<<"Please enter the number of pins down on this roll"<<endl;
cin>>(*pr1);
}
}
*pgamescore += framescore;
*pframe += 1;
}
void
update(int gamescore,int *pnumber,int *pmin,int *pmax,int *ptotal)
{
cout<<"gamescore "<<gamescore<<endl;
*pnumber += 1;
*ptotal += gamescore;
if(gamescore < *pmin)
*pmin = gamescore;
if(gamescore > *pmax)
*pmax = gamescore;
}