Cambridge AS Level Computer Science 9618 · 11.1 Programming Basics

Q30 · 9618/23/O/N/24 Q6

Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3.

Q309618/23/O/N/24original Q6Structured · 7 marks
Question 6 from 9618/23/O/N/24

Worked solution

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
ENDFUNCTION

Function 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.

KEY IDEAUse SETDATE to build each March date and DAYINDEX to spot Sundays until the third one is found.
TOTAL 7 marks

Official mark scheme

Mark scheme for 9618/23/O/N/24 question 6
VIDEO
▶ 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.