222k views
3 votes
Trace the following pseudocode. List the values of COUNTER and ANSWER throughout and then show the final values.

a. Set COUNTER to 0.
b. Set ANSWER to 100.
c. Divide ANSWER by 2 and set ANSWER to the new result
d. If ANSWER is even, go back to step 3 and add 1 to COUNTER. If ANSWER is odd, go to the next line.
e. Repeat lines 6 and 7 until COUNTER is greater than 3
f. Increase ANSWER by the value of COUNTER.
g. Increase COUNTER by 1.
h. While COUNTER is greater than or equal to 2 do lines 9 and 10.
i. Decrease ANSWER by 5
j. Decrease COUNTER by 1
k. Display ANSWER and COUNTER

User Etherice
by
5.6k points

2 Answers

4 votes

Final answer:

The pseudocode describes a series of steps involving the manipulation of COUNTER and ANSWER variables, leading to a final output after various loops and conditional actions. The final values are ANSWER = 16 and COUNTER = 1.

Step-by-step explanation:

The pseudocode given describes a sequence of steps that manipulate two variables, COUNTER and ANSWER. Initially, COUNTER is set to 0, and ANSWER is set to 100. The loop that follows divides ANSWER by 2, adds 1 to COUNTER if ANSWER is even, and continues until COUNTER is greater than 3. There's another loop where ANSWER is increased by COUNTER, and then ANSWER is decreased by 5 as long as COUNTER is greater than or equal to 2, with counter decreasing each time by 1. This repeated sequence will ultimately display the final values of ANSWER and COUNTER.

Tracing the pseudocode:

  1. Initial values: COUNTER = 0, ANSWER = 100
  2. ANSWER is halved to 50, COUNTER remains 0.
  3. Since 50 is even, return to step 3: now ANSWER is 25 and COUNTER is 1.
  4. ANSWER is now odd, so proceed to step 5. Since COUNTER is not greater than 3, it increases ANSWER by 1 (26) and increments COUNTER by 1 (2).
  5. Since COUNTER is now 2, step 7 proceeds: ANSWER is increased by COUNTER (28), and COUNTER is incremented by 1 (3).
  6. The loop of step 6-7 continues increasing ANSWER by 1 and COUNTER by 1, until COUNTER becomes 4.
  7. Since COUNTER is 4, we now execute steps 8-10 where ANSWER is decreased by 5 (31) and COUNTER by 1 (3).
  8. This step is repeated until COUNTER is less than 2.
  9. The final output will be displayed as ANSWER and COUNTER, which at the end will be 16 and 1, respectively.

User CommanderCat
by
5.6k points
3 votes

Answer:

ANSWER = 16

COUNTER = 1

Step-by-step explanation:

a. COUNTER = 0

b. ANSWER = 100

c. ANSWER = 100/2 = 50

d. ANSWER (50) is even, so it goes back to (c)

ANSWER = 50/2 = 25

and

COUNTER = COUNTER + 1 = 0 + 1 =1

ANSWER is now odd (25), so it moves to (e)

e. f and g will be executed as follows.

f. ANSWER = ANSWER + COUNTER = 25 + 1 = 26

g. COUNTER = COUNTER+1 = 1 + 1 = 2.

COUNTER is not greater than 3, so f & g will be executed again

f. ANSWER = 26 + 2 = 28

g. COUNTER = 2 + 1 = 3.

And again

f. ANSWER = 28 + 3 = 31

g. COUNTER = 3 + 1 = 4

Now, execution will proceed to h

h. i and j will be executed as follows

i. ANSWER = ANSWER - 5 = 31 - 5 = 26

j. COUNTER = COUNTER - 1 = 4 - 1 = 3

COUNTER ≥ 2, so it'll be executed again

i. ANSWER = 26 - 5 = 21

j. COUNTER = 3 - 1 = 2

And again...

i. ANSWER = 21 - 5 = 16

j. COUNTER = 2 - 1 = 1

Now, the condition is no longer valid.

So, execution proceed to k

k. The displayed values of ANSWER and COUNTER is 16 and 1, respectively.

User Cartalot
by
5.2k points