Solved 24-780 Engineering Computation Problem Set 10

$30.00

Original Work ?

Download Details:

  • Name: Assignment-10-ialk4h.zip
  • Type: zip
  • Size: 252.73 KB

Category: Tags: , , , You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

Rate this product

PS10 Bowling Score Calculator (ps10.cpp)
The goal of the bowling game is to knock down all ten pins with your ball. One game consists of ten frames, and
the bowler can throw a ball up to twice to knock down all the pins. In the last (10th) frame, the bowler is allowed
to throw up to three shots if the bowler knocks down all ten pins within the first two attempt of the frame.
Therefore, in a single game, the bowler may throw up to 21 shots.
If the bowler fails to knock down all pins in the frame, the score earned from the frame is the number of pins that
falls down.
If the bowler knocks down all ten pins in the first attempt of the frame (called strike), the bowler does not throw
the second shot of the frame, and the score earned from the frame is 10 plus number of pins from the next two
shots. For example, if the bowler knocks all the pins in the first attempt of the first frame, and 8 pins and 1 pin in
the two shots of the second frame, the total score earned in the first frame is 19. Or, if the bowler knocks all the
pins in the first attempt of the first and the second frame and knocks 9 pins in the first attempt of the third frame,
total score earned in the first frame is 29. If the bowler gets three strikes in a row, the score earned for the first
strike is 30.
If the bowler fails to knock down all ten pins in the first attempt, but knocks down the remaining pins in the second
attempt of the frame (called spare), the total score earned from the frame is 10 plus number of pins from the next
one shot. For example, if the bowler knocks 9 pins and 1 pin in the first and the second shot of the first frame
respectively, and gets a strike (10 pins in the first attempt) in the second frame, total score earned from the first
frame is 20.
If the bowler gets a strike in the first shot of the 10th frame, the bowler is allowed to shoot twice more in the same
frame. Or if the bowler gets a spare in the first two attempt of the 10th frame, the bowler is allowed to shoot once
more in the same frame. The total score earned in the 10th frame is equal to the total pins that the bowler knocks
down in the frame. (The maximum is 30 pins when the bowler gets three strikes in a row in the 10th frame). Even
when the bowler gets a strike in the third shot of the 10th frame, or a spare in the second and the third shots of
the 10th frame, there is no 11th frame.
Total score from the game is the total of the scores from all ten frames.
Write a program that takes one string from the console window and calculates the bowling score from the string.
The program must first prompt the user by printing:
Enter Frames>
This one pin destroyed my
perfect game!
The format of the string is as follows:
‘0’ to ‘9’ Number of pins knocked down
‘X’ or ‘x’ Strike
‘/’ Spare
‘-‘ Same as ‘0’
‘ ‘ (Space) Same as ‘0’
The input string must be always at least 21 letters long. If it is shorter than 21 letters, your program must print an
error message. If it is longer than 21 letters, your program should ignore letters beyond 22nd letter. Also, if the
input string includes any other character other than listed above, your program should print an error and
terminate. Your program does NOT have to check for other errors like saying more than 10 pins are knocked in
one frame.
Each two letters indicate how the bowler knocked the pins in the each frame except that the last three of the 21
letters indicate how the bowler knocked the pins in the 10th frame.
For example:
X X X X X X X X X XXX
means perfect game and the score is 300. Or,
9/9/9/9/9/9/9/9/9/9/9
means the bowler had 9+1 pins in each frame, and the last extra shot of the 10th frame was 9. Or,
9-9-9-9-9-9-9-9-9-9–
should be calculated as 90.
Your program must recognize “91” and “9/” the same. (‘/’ must be considered as 10 minus previous pins.)
The output needs to be one line “The score is” followed by the calculated score in the next line, for example:
The score is:
180
Your program must terminate after showing the score for the game.
Your program will be graded based on how many accurate calculations your program gives for our test cases.
Suggested steps (you don’t have to follow if you come up with easier steps):
(1) Write a program that takes a single line from the console. Use TextString class and use Fgets member
function to take input from stdin. You can use any version of TextString class. Show an error and terminate if
the string is too short, including unrecognizable character.
(2) Interpret 21 letters to 21 integers. For example, turn:
“9/72X-X-8-919/9/9-XX6”
into an array of integers:
9, 1, 7, 2, 10, 0, 10, 0, 8, 0, 9, 1, 9, 1, 9, 1, 9, 0, 10, 10, 6
(3) Write a function int ScoreFromFrame(const int pinsKnocked[21],int frameNo); (Hint: You may want to write
calculations for1-9th frames, and 10th frame separately. And maybe you want to write separate calculation
for the strike in the 9th frame.)
(4) Sum up ScoreFromFrame(pinsKnocked,i) for i=0 to 9 (or i=1 to10 depending on how you write your
ScoreFromFrame), and then print.