Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Ana saves money each week. This pseudocode works out her savings in dollars.
DECLARE Week : INTEGER
DECLARE Saved : INTEGER
Saved ← 5
FOR Week ← 2 TO 6
Saved ← Saved + Week * 2
NEXT Week
OUTPUT SavedHow many times does the loop run, and what is output?
FOR Count ← A TO B STEP S ... NEXT Count runs a known number of times. WHILE <condition> DO ... ENDWHILE tests first and may run no times. REPEAT ... UNTIL <condition> tests last and runs at least once. A total adds each value; a count adds 1. A trace table records every variable and output at each step.
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. The passes. Week goes from 2 to 6 in steps of 1: that is 5 passes.
Step 2. The total. Saved starts at 5. It gains , , , and :
Answer: 5 passes, and the output is 45.
Check. The values added are , and .
Log in to join the discussion.
Loading…