File: bowling.txt 8 1 X X 3 / 4 5 1 / X X 3 / 4 / 8 X X X X X X X X X X 9 / 2. T
ID: 3756670 • Letter: F
Question
File: bowling.txt
8 1 X X 3 / 4 5 1 / X X 3 / 4 / 8
X X X X X X X X X X 9 /
2. The game of bowling consists of ten frames. For each frame, ten pins are set at the end of a lane. The player's objective is to knock down all the pins by rolling a ball down the lane. The bowler is allowed up to two rolls to knock down all the pins. If the bowler knocks them all down on the first attempt, the frame is scored as a strike. If the bowler does not knock them down on the first attempt in the frame the bowler is allowed a second attempt to knock down the remaining pins. If the bowler succeeds in knocking the rest of the pins down in the second attempt, the frame is scored as a spare. The score for a bowling game consists of a sum of the scores for each frame. The score for frames 1-9 will be the total number of pins knocked down in the frame, plus bonuses for strikes and spares. If a bowler scores a strike in a frame, the score for that frame is ten plus the sum of the next two rolls. If a bowler scores a spare in a frame, the score for that frame is ten plus the score of the next roll.Explanation / Answer
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int frame[10][3] = {{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0,0}};
int frameOutcome[10];
int frameTotal[10];
int currentFrame = 0;
int totalScore = 0;
do
{
cout << "Input roll 1 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][0];
cout << endl;
if (frame[currentFrame][0] == 10)
{
cout << "STRIKE" << endl;
frameOutcome[currentFrame] = 2;
currentFrame++;
}
else
{
cout << "Input roll 2 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][1];
cout << endl;
if (frame[currentFrame][0] + frame[currentFrame][1] == 10)
{
cout << "SPARE!" << endl;
frameOutcome[currentFrame] = 1;
currentFrame++;
}
else
{
cout << "The total for frame " << currentFrame + 1 <<
" is: " << frame[currentFrame][0] + frame[currentFrame][1] << endl;
frameOutcome[currentFrame] = 0;
currentFrame++;
}
}
}
while (currentFrame < 9);
do
{
cout << "Input roll 1 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][0];
cout << endl;
if (frame[currentFrame][0] == 10)
{
cout << "STRIKE" << endl;
frameOutcome[currentFrame] = 2;
cout << "Input roll 2 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][1];
cout << endl;
cout << "Input roll 3 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][2];
cout << endl;
currentFrame++;
}
else
{
cout << "Input roll 2 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][1];
cout << endl;
if (frame[currentFrame][0] + frame[currentFrame][1] == 10)
{
cout << "SPARE!" << endl;
frameOutcome[currentFrame] = 1;
cout << "Input roll 3 for frame number " << currentFrame + 1 << endl;
cin >> frame[currentFrame][2];
cout << endl;
currentFrame++;
}
else
{
cout << "The total for frame " << currentFrame + 1 <<
" is: " << frame[currentFrame][0] + frame[currentFrame][1] << endl;
frameOutcome[currentFrame] = 0;
currentFrame++;
}
}
}
while (currentFrame < 10);
for (currentFrame = 0; currentFrame < 8 ; currentFrame++)
{
switch(frameOutcome[currentFrame])
{
case 0:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1];
break;
case 1:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1] +
frame[currentFrame + 1][0];
break;
case 2:
if(frameOutcome[currentFrame + 1] == 2)
{
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame + 1][0] +
frame[currentFrame + 2][0];
}
else
{
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame + 1][0] +
frame[currentFrame + 1][1];
}
break;
}
}
for (currentFrame = 8; currentFrame < 9 ; currentFrame++)
{
switch(frameOutcome[currentFrame])
{
case 0:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1];
break;
case 1:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1] +
frame[currentFrame + 1][0];
break;
case 2:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame + 1][0] +
frame[currentFrame + 1][1];
break;
}
}
for (currentFrame = 9; currentFrame < 10 ; currentFrame++)
{
switch(frameOutcome[currentFrame])
{
case 0:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1];
break;
case 1:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1] +
frame[currentFrame][2];
break;
case 2:
frameTotal[currentFrame] = frame[currentFrame][0] + frame[currentFrame][1] +
frame[currentFrame][2];
break;
}
}
for (currentFrame = 0; currentFrame < 10 ; currentFrame++)
{
totalScore = totalScore + frameTotal[currentFrame];
cout << "Frame " << currentFrame + 1 << " Total score: " <<
frameTotal[currentFrame];
cout << endl;
}
cout << "Total score = " << totalScore << endl;
system("PAUSE");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.