Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3.
Count the days of March one at a time. Make a date with SETDATE, check if it is a Sunday with DAYINDEX (Sunday is 1), and stop at the third Sunday.
FUNCTION AdjustClock(ThisYear : INTEGER) RETURNS INTEGER
DECLARE ThisDayNumber, SundayCount : INTEGER
DECLARE ThisDate : DATE
ThisDayNumber ← 0
SundayCount ← 0
REPEAT
ThisDayNumber ← ThisDayNumber + 1
ThisDate ← SETDATE(ThisDayNumber, 3, ThisYear)
IF DAYINDEX(ThisDate) = 1 THEN
SundayCount ← SundayCount + 1
ENDIF
UNTIL SundayCount = 3
RETURN ThisDayNumber
ENDFUNCTIONFunction heading, parameter, return type INTEGER and ENDFUNCTION [1]
Local integer variable declared to make the date (day number) [1]
Loop that repeats until the third Sunday is found [1]
Date made correctly with SETDATE(day, 3, year) inside the loop [1]
Test for Sunday using DAYINDEX() inside the loop [1]
Sunday count started at 0 before the loop and increased inside it [1]
Day number of the third Sunday returned [1]
Another accepted way has no loop: use SETDATE(1, 3, ThisYear) and DAYINDEX to find the weekday of 1 March, then work out the third Sunday for each of the seven possible weekdays.
▶ Whole paper
Video solution of the whole paper · by Zafar Ali Khan (ZAK). 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.