Pseudocode Fragment Question

PeterRobinson44

New member
Joined
May 14, 2020
Messages
12
The answer is 32. However, I am trying to determine how many times the count changes in the problem. Is it 5 because it goes from 4 to 3 to 2 to 1 to 0? Or 4 because it goes from 4 to 3 to 2 to 1? I am overthinking this question and would appreciate any help.

Given the pseudocode fragment:

x: = 2
count: = 4
while (count > 0)
x: = 2 * x
count: = count - 1
End-while

What is the final value for x?

x = 2
count = 4
4 > 0
x: 2 * 2 = 4
count = 3

x = 4
count = 3
3 > 0
x: 2 * 4 = 8
count = 2

x = 8
count = 2
2 > 0
x: 2 * 8 = 16
count = 1

x = 16
count = 1
1 > 0
x: 2 * 16 = 32
count = 0
 
The answer is 32. However, I am trying to determine how many times the count changes in the problem. Is it 5 because it goes from 4 to 3 to 2 to 1 to 0? Or 4 because it goes from 4 to 3 to 2 to 1? I am overthinking this question and would appreciate any help.

Given the pseudocode fragment:

x: = 2
count: = 4
while (count > 0)
x: = 2 * x
count: = count - 1
End-while

What is the final value for x?

x = 2
count = 4
4 > 0
x: 2 * 2 = 4
count = 3

x = 4
count = 3
3 > 0
x: 2 * 4 = 8
count = 2

x = 8
count = 2
2 > 0
x: 2 * 8 = 16
count = 1

x = 16
count = 1
1 > 0
x: 2 * 16 = 32
count = 0
If this is a question you were given, please quote the question exactly, so we can be sure what is being asked for. This is largely a matter of the meaning of the words.

But count changes 4 times: from 4 to 3, from 3 to 2, from 2 to 1, and from 1 to 0. Just count the times you went through the loop and changed the value of count.
 
Top