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

Respuesta :

Answer:

ANSWER = 16

COUNTER = 1

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.