Question

1. The table below contains a program and a sample input, complete the table by writing the output that will result from the input. [Be careful and follow every line as it would execute on a computer, don't assume you can just guess how the game works, unless you do know snooker.]

2. List the counters and accumulators (if any) in the program, briefly explain why you think each is a counter or accumulator.

input:

O'Sullivan
red
green
red
pink
red
none

Program Input Output? start num RED POINTS 1 num YELLOW POINTS2 num GREEN POINTS 3 num BROWN POINTS 4 num BLUE PONTS 5 num PI

start
    num RED_POINTS = 1
    num YELLOW_POINTS = 2
    num GREEN_POINTS = 3
    num BROWN_POINTS = 4
    num BLUE_POINTS = 5
    num PINK_POINTS = 6
    num BLACK_POINTS = 7
    num redCount = 0
    num yellowCount = 0
    num greenCount = 0
    num brownCount = 0
    num blueCount = 0
    num pinkCount = 0
    num blackCount = 0
    num colorCount = 0
    num score = 0
    string ballPotted
    boolean failedToPot = false
    string playerName
    input playerName
    while NOT(failedToPot)
        input ballPotted
        if ballPotted = "red"
            redCount = redCount + 1
            score = score + RED_POINTS
        else
            if ballPotted = "yellow"
                yellowCount = yellowCount + 1
                colorCount = colorCount + 1
                score = score + YELLOW_POINTS
            else
                if ballPotted = "green"
                    greenCount = greenCount + 1
                    colorCount = colorCount + 1
                    score = score + GREEN_POINTS
                else
                    if ballPotted = "brown"
                        browCount = brownCount + 1
                        colorCount = colorCount + 1
                        score = score + BROWN_POINTS
                    else
                        if ballPotted = "blue"
                            blueCount = blueCount + 1
                            colorCount = colorCount + 1
                            score = score + BLUE_POINTS
                        else
                            if ballPotted = "pink"
                                pinkCount = pinkCount + 1
                                colorCount = colorCount + 1
                                score = score + PINK_POINTS
                            else
                                if ballPotted = "black"
                                    blackCount = blackCount + 1
                                    colorCount = colorCount + 1
                                    score = score + BLACK_POINTS
                                else
                                    output playerName
                                    output score
                                    failedToPot = true
                                endif
                            endif
                        endif
                    endif
                endif
            endif 
        endif
    endwhile
    output "Color balls potted: ", colorCount
stop
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I'm gonna start explaining the program and every input to you one by one so just keep the code at one side and read this answer. The actual lines of code are in bold and each line of my explanation is normal text.

Start // Here the program just starts

So, the next few lines they’ve declared a bunch of variables and assigned some value to some of them.

    num RED_POINTS = 1 // declaring and initialising a number variable RED_POINTS to value 1

    num YELLOW_POINTS = 2 // declaring and initialising a number variable YELLOW_POINTS to value 2

    num GREEN_POINTS = 3 // declaring and initialising a number variable GREEN_POINTS to value 3

    num BROWN_POINTS = 4 // declaring and initialising a number variable BROWN_POINTS to value 4

    num BLUE_POINTS = 5 // declaring and initialising a number variable BLUE_POINTS to value 5

    num PINK_POINTS = 6 // declaring and initialising a number variable PINK_POINTS to value 6

   num BLACK_POINTS = 7 // declaring and initialising a number variable BLACK_POINTS to value 7

    num redCount = 0 // declaring and initialising a number variable redCount to value 0

    num yellowCount = 0 // declaring and initialising a number variable yellowCount to value 0

  num greenCount = 0 // declaring and initialising a number variable greenCount to value 0

    num brownCount = 0 // declaring and initialising a number variable brownCount to value 0

    num blueCount = 0 // declaring and initialising a number variable blueCount to value 0

    num pinkCount = 0 // declaring and initialising a number variable pinkCount to value 0

    num blackCount = 0 // declaring and initialising a number variable blackCount to value 0

    num colorCount = 0 // declaring and initialising a number variable colorCount to value 0

    num score = 0 // Setting a number variable score to value 0

    string ballPotted // declaring a string ballPotted

    boolean failedToPot = false // declaring a boolean failedToPot to false

    string playerName // decalring a string playerName

    input playerName // taking input playerName. Here the first input O'Sullivan is taken as input and stored in the variable playerName

So, now there’s a while loop which will keep going if the value of failedToPot is false. As the initial value of failed to pot is false and the NOT makes it true the loop with start.

    while NOT(failedToPot) // starting the loop

        input ballPotted // taking input and storing in variable ballPotted

        if ballPotted = "red" //if the input is red then enter the if statement

            redCount = redCount + 1 // increase the variable redCount by 1

            score = score + RED_POINTS // update variable score to score + RED_POINTS

        else

            if ballPotted = "yellow" //if the input is not red but yellow then enter the else if statement

                yellowCount = yellowCount + 1 // increase the variable yellowCount by 1

                colorCount = colorCount + 1 // increase the variable colorCount by 1

                score = score + YELLOW_POINTS // update variable score to score + YELLOW_POINTS

            else

                if ballPotted = "green" //if the input is neither red or yellow but green then enter the else if statement

                    greenCount = greenCount + 1 // increase the variable greenCount by 1

                    colorCount = colorCount + 1 // increase the variable colorCount by 1

                    score = score + GREEN_POINTS // update variable score to score + GREEN_POINTS

                else

                    if ballPotted = "brown" //if the input is neither red, yellow or green but brown then enter the else if statement

                        browCount = brownCount + 1 // increase the variable brownCount by 1

                        colorCount = colorCount + 1 // increase the variable colorCount by 1

                        score = score + BROWN_POINTS // update variable score to score + BROWN_POINTS

                    else

                        if ballPotted = "blue" //if the input is neither red, yellow, green or brown but blue then enter the else if statement

                            blueCount = blueCount + 1 // increase the variable blueCount by 1

                            colorCount = colorCount + 1 // increase the variable colorCount by 1

                            score = score + BLUE_POINTS // update variable score to score + BLUE_POINTS

                        else

                           if ballPotted = "pink" //if the input is neither red, yellow, green, brown or blue but pink then enter the else if statement

                                pinkCount = pinkCount + 1 // increase the variable pinkCount by 1

                                colorCount = colorCount + 1 // increase the variable colorCount by 1

                                score = score + PINK_POINTS // update variable score to score + PINK_POINTS

                            else

                                if ballPotted = "black" //if the input is neither red, yellow, green, brown, blue or pink but black then enter the else if statement

                                    blackCount = blackCount + 1 // increase the variable blackCount by 1

                                    colorCount = colorCount + 1 // increase the variable colorCount by 1

                                    score = score + BLACK_POINTS // update variable score to score + BLACK_POINTS

                                else //if the input is anything except red, yellow, green, brown, blue, pink or black then enter the else statement

                                    output playerName // output the playerName variable

                                    output score // output the score variable

                                    failedToPot = true // set the failedToPot variable to true.

                                Endif // now the program closes all the if statements one by one.

                            Endif // closing if

                        endif // closing if

                    endif // closing if

                endif // closing if

            endif // closing if

        endif // closing if

    endwhile // end of the while loop

    output "Color balls potted: ", colorCount // output the text “Color balls potted: ” followed by the variable colorCount

stop // the program stops here.

The first input (O’Sullivan): This program first takes in the name of the player in the input.

Second Input (red): This input is inside the while loop. And it matches the condition of one of the if statements so it goes inside the if statement and increases redCount(0) by 1, so redCount now = 1. And updates the score(0) by score(0) + RED_POINTS(1), so score = 1 now.

Third Input (green): This input is again inside the while loop. And it matches the condition of one of the if statements so it goes inside the if statement and increases greenCount(0) by 1, so greenCount now = 1. It also increases the colorCount(0) by 1, so colorCount now = 1. And updates the score(1) by score(1) + GREEN_POINTS(3), so score = 4 now.

Fourth Input (red): This input is again inside the while loop. And it matches the condition of one of the if statements so it goes inside the if statement and increases redCount(1) by 1, so redCount now = 2. And updates the score(4) by score(4) + RED_POINTS(1), so score = 5 now.

Fifth Input (pink): This input is again inside the while loop. And it matches the condition of one of the if statements so it goes inside the if statement and increases pinkCount(0) by 1, so pinkCount now = 1. It also increases the colorCount(1) by 1, so colorCount now = 2. And updates the score(5) by score(5) + PINK_POINTS(6), so score = 11 now.

Sixth Input (red): This input is again inside the while loop. And it matches the condition of one of the if statements so it goes inside the if statement and increases redCount(2) by 1, so redCount now = 3. And updates the score(11) by score(11) + RED_POINTS(1), so score = 12 now.

Seventh Input (none): This input is again inside the while loop but it does not match the conditions of any of the if statements, so it goes inside the else statement and output the variable playerName. Then it outputs the variable score. And finally sets the failedToPot variable to true.

Next time the while loop runs the failedToPot variable is set to true and the NOT will turn it into false. So the while loop ends and the following line executes:

output "Color balls potted: ", colorCount

This line outputs the text “Color balls potted: “ followed by the variable colorCount. And the program ends here.

So, the final output is:

O’Sullivan

12

Color balls potted: 2

Add a comment
Know the answer?
Add Answer to:
1. The table below contains a program and a sample input, complete the table by writing the output that will result from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 10 1 pts What will be the output of the following code: Scolors-arrayC" red", "green",...

    Question 10 1 pts What will be the output of the following code: Scolors-arrayC" red", "green", "blue", "yellow"; foreach (Scolors as Svalue)t echo "$value<br />"; O red, green, blue, yellow O Nothing will appear red red green blue yellow Question 9 1 pts The following code block creates what kind of form input: <select name="age"> <option <option <option <option <option value-"0-17">Under 18</option> value="18-25">Between 18 and 25</option> value="26-45">Between 26 and 45</option> value="46-60">Between 46 and 60</option> value="60+">Over 60</option> </select> O Checkboxes Dropdown...

  • Write in coral please QUESTION 7 5 points Save Answer Complete the program below to convert...

    Write in coral please QUESTION 7 5 points Save Answer Complete the program below to convert water temperatures from degrees Celsius to degrees Fahrenheit. Then, warn the swimmers if the water temperature is too cold or too hot. If the water temperature is 77 degrees Fahrenheit or less, it is too cold to swim. If the water is 104 degrees Fahrenheit or more, it is too hot to swim. Example 1: input: 55 output: Water temperature is 131.0 degrees Fahrenheit....

  • int main() { int a, Band3 = 0, Band1, Band2, b, c = 1; float t;...

    int main() { int a, Band3 = 0, Band1, Band2, b, c = 1; float t; while (c) { Band3 = 0; printf(" Enter Resistance Value: "); scanf_s("%d", &a); printf("\n Enter Tolerance Value: "); scanf_s("%f", &t); b = t * 100; while (a % 10 == 0) { Band3++; a = a / 10; } Band2 = a % 10; a = a / 10; Band1 = a % 10; printf("\n Band1 = %d, Band2 = %d, Band3 = %d...

  • Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design:...

    Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design: You will create a class and then use the provided test program to make sure it works. This means that your class and methods must match the names used in the test program. Also, you need to break your class implementation into multiple files. You should have a car.h that defines the car class, a list.h, and a list.cpp. The link is a struct...

  • I am currently trying to figure out the experiment below. Please complete Table 1 with an...

    I am currently trying to figure out the experiment below. Please complete Table 1 with an explanation, I appreciate it thank you!  Promise to give thumbs up! Introduction The phase differences between the output voltage, the voltage across the inductor, the voltage across the capacitor, and the voltage across the resistor will be examined at resonant frequency. The voltage and phase relationship will also be examined for frequencies above and below resonance. Theory An inductor, a capacitor, and a resistor are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT