Worked solution from the Algorithm Design and Problem-solving Paper 2 Problem-solving and Programming booklet, Part 2 of 2.
Follow the arrows: the decision "Is Index < 7" with the arrow looping back is a WHILE loop. The decision "Is Average > Last" is an IF inside it.
INPUT UserID
Average ← GetAverage(UserID)
Total ← 0
Index ← 4
WHILE Index < 7
Last ← SameMonth[Index]
IF Average > Last THEN
Total ← Total + Average
ELSE
Total ← Total + Last
ENDIF
Index ← Index + 1
ENDWHILE
CALL Update(UserID, Total)UserID input, and Average assigned from GetAverage(UserID) [1]
Total set to 0 and Index set to 4 [1]
Conditional loop that runs for Index from 4 to 6 [1]
Last assigned from SameMonth[Index] inside the loop [1]
IF … THEN … ELSE … ENDIF inside the loop [1]
Correct assignments to Total, Index incremented, and CALL Update(UserID, Total) after the loop [1]
A FOR Index ← 4 TO 6 … NEXT Index loop also scores, and a REPEAT … UNTIL Index = 7 loop also scores.
Write: pre-condition loop (also accepted: count-controlled loop). The test is made before the loop body. [1]
▶ Whole paper
Video solution of the whole paper · by Audere Prep. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.