Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
This algorithm adds up positive numbers until it has three of them. It shows a prompt before each input.
Count ← 0
Sum ← 0
REPEAT
OUTPUT "Enter a number"
INPUT Num
IF Num > 0
THEN
Sum ← Sum + Num
Count ← Count + 1
ENDIF
UNTIL Count = 3
OUTPUT "Sum: ", SumComplete a trace table with the columns Count, Sum, Num and OUTPUT for the inputs , , , , . How many times is the prompt output, and what is the final value of Sum?
A dry run follows an algorithm by hand. A trace table has a column for each variable and for the output, which includes the user prompts. Write a value only when it changes, and start a new row for each pass of a loop.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. Start. Count , Sum .
Step 2. The passes. Each pass starts a new row with the prompt in the OUTPUT column.
Sum , Count .Sum , Count .Sum , Count . Now Count = 3, so the loop stops.Step 3. Output. Sum: 15.
Answer: the prompt is output times, once for each input, and Sum ends at .
Check. Three positive numbers were added: .
Log in to join the discussion.
Loading…