Write a complete C++ program that inputs three numbers representing the number o
ID: 3803679 • Letter: W
Question
Write a complete C++ program that inputs three numbers representing the number of pins knocked down by a bowler in three throws. Your program should output the computed bowling score, and check for possible input errors. For example, a throw may be in the range of 0 through 10 points, and the total of the first 2 throws must be less than or equal to 10. except when the 1st throw is a strike. the rules of bowling are that if the first throw is a strike (all 10 pins knocked down), then the score is equal to those 10 points plus the number knocked down in the next 2 rows. Thus, the maximum score (3 strikes) is 30. If the 1st throw knocks down fewer than 10 pins, but the second throw knocks down the remainder of the 10 pins (a spare), then the score is those 10 points plus the number of pins knocked down on the third throw. If the first 2 throws fail to knock down all of the pins (a blow), then the score is just the total number of pins knocked down in the first 2 throws.Explanation / Answer
Please refer below code
#include<iostream>
using namespace std;
int score(int first_throw, int second_throw,int third_throw)
{
int total_score = -1;
//if input score is negative or greater than 10 or sum of all score is > 10
if(first_throw < 0 || first_throw > 10 || second_throw < 0 || second_throw > 10 ||
third_throw < 0 || third_throw > 10 || (first_throw + second_throw + third_throw) > 30)
return total_score;
else
{
if(first_throw == 10) //If first throw is strike
total_score = 10 + second_throw + third_throw;
//If first throw knocks down fewer than 10 and second knocks down remaining
else if((first_throw < 10) && (second_throw == 10 - first_throw))
total_score = 10 + third_throw;
else //If all pins not knocked down in first two throws
total_score = first_throw + second_throw;
}
return total_score;
}
int main()
{
int first_throw,second_throw,third_throw,total_score;
cout<<"Please three numbers representing number of pins knocked down : ";
cin>>first_throw>>second_throw>>third_throw;
total_score = score(first_throw,second_throw,third_throw);
if(total_score < 0)
cout<<"Invalid Input "<<endl;
else
cout<<"Total Score : "<<total_score;
return 0;
}
Please refer below output
Please three numbers representing number of pins knocked down : 8 2 5
Total Score : 15
Process returned 0 (0x0) execution time : 7.938 s
Press any key to continue.
Please three numbers representing number of pins knocked down : 10 3 7
Total Score : 20
Process returned 0 (0x0) execution time : 4.915 s
Press any key to continue.
Please three numbers representing number of pins knocked down : 4 7 1
Invalid Input
Process returned 0 (0x0) execution time : 10.751 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.