Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A long scenario question. At a sports day, six runners each run 100 metres. Write a program that:
Use constants for the number of runners and the 15.0 seconds. You may answer in pseudocode or Python. Then dry-run it for the typed times 16.4, 14.2, 35.0, 13.8, 15.0, 17.9 and 14.9.
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.
A model answer in pseudocode.
CONSTANT Runners ← 6
CONSTANT Qualify ← 15.0
DECLARE Runner : INTEGER
DECLARE Time : REAL
DECLARE Total : REAL
DECLARE Fastest : REAL
DECLARE Qualified : INTEGER
Total ← 0.0
Qualified ← 0
FOR Runner ← 1 TO Runners
REPEAT
OUTPUT "Time for runner ", Runner
INPUT Time
IF Time < 9.0 OR Time > 30.0
THEN
OUTPUT "Time out of range"
ENDIF
UNTIL Time >= 9.0 AND Time <= 30.0
Total ← Total + Time
IF Runner = 1
THEN
Fastest ← Time
ELSE
IF Time < Fastest
THEN
Fastest ← Time
ENDIF
ENDIF
IF Time < Qualify
THEN
Qualified ← Qualified + 1
ENDIF
NEXT Runner
OUTPUT "Fastest time ", Fastest
OUTPUT "Average time ", ROUND(Total / Runners, 2)
OUTPUT "Runners under ", Qualify, " seconds ", QualifiedThe dry run. The time 35.0 is rejected, so runner 3 is asked again and types 13.8. The six accepted times are 16.4, 14.2, 13.8, 15.0, 17.9 and 14.9. Their total is 92.2, so the average is , which rounds to 15.37. The fastest is 13.8. The times under 15.0 are 14.2, 13.8 and 14.9, so the count is 3. The time 15.0 itself is not under 15.0.
How such an answer is marked. One mark for each point, up to 15:
Watch out. Working out the average inside the loop, or setting the total to zero inside it, are the two most common slips.
Log in to join the discussion.
Loading…