9618/23/O/N/24 question 6: worked solution (October/November 2024, paper 23)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 11.1 Programming Basics Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3. Q30 9618/23/O/N/24 original Q6 Structured · 7 marks 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 IDEA Use SETDATE to build each March date and DAYINDEX to spot Sundays until the third one is found. TOTAL 7 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.