9618/23/M/J/25 question 7: worked solution (May/June 2025, paper 23)

Show as plain text

Cambridge AS Level Computer Science 9618 · 11.3 Structured Programming Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 3 of 3. Q91 9618/23/M/J/25 original Q7 Structured · 18 marks Worked solution (a)(i) Both values must be changed and kept after the call, so pass both parameters BYREF . Use TODAY() for the current date and MONTH() to compare months. PROCEDURE UpdateVisit(BYREF LoyaltyPoints : INTEGER, BYREF LastVisitDate : DATE) IF MONTH(TODAY()) = MONTH(LastVisitDate) THEN LoyaltyPoints ← LoyaltyPoints + 4 ELSE LoyaltyPoints ← LoyaltyPoints + 1 ENDIF LastVisitDate ← TODAY() ENDPROCEDURE Procedure heading and ending with two parameters of the correct types [1] Both parameters passed BYREF [1] MONTH() used on the last visit date and on TODAY() [1] Add 4 if the months match, otherwise add 1 [1] LastVisitDate set to TODAY() [1] (a)(ii) Compare the day of the week of the two dates: DAYINDEX(LastVisitDate) = DAYINDEX(TODAY()) Condition comparing DAYINDEX of both dates [1] (b)(i) The line looks like 100123,132,05032025. The ID and comma take 7 characters, so the points start at position 8. The comma and the date at the end take 9 characters, so the points are LENGTH(Line) − 16 characters long. Monday is DAYINDEX 2 (Sunday is 1). FUNCTION MondayCheck(CustomerID : STRING) RETURNS INTEGER DECLARE Line, LoyaltyPointsString : STRING DECLARE LoyaltyPoints : INTEGER Line ← FindCustomer(CustomerID) LoyaltyPointsString ← MID(Line, 8, LENGTH(Line) - 16) LoyaltyPoints ← STR_TO_NUM(LoyaltyPointsString) IF DAYINDEX(TODAY()) = 2 THEN LoyaltyPoints ← LoyaltyPoints + 10 ENDIF RETURN LoyaltyPoints ENDFUNCTION FindCustomer(CustomerID) called and its value stored [1] MID function used [1] Attempt to extract the loyalty points from the line [1] Loyalty points extracted correctly [1] Extracted string converted to an integer [1] Check that today is a Monday using TODAY() [1] Add 10 to the loyalty points if it is [1] Return the loyalty points [1] Another way to find the end of the points also scores: a WHILE loop that looks for the next comma. (b)(ii) Pick out the day, month and year with substring functions, convert each to an integer, then build the date: DayInt ← STR_TO_NUM(LEFT(LastVisitDateString, 2)) MonthInt ← STR_TO_NUM(MID(LastVisitDateString, 3, 2)) YearInt ← STR_TO_NUM(RIGHT(LastVisitDateString, 4)) LastVisitDate ← SETDATE(DayInt, MonthInt, YearInt) A substring function used (LEFT, MID or RIGHT) [1] Day, month and year all extracted correctly [1] STR_TO_NUM used three times [1] SETDATE used and the result assigned to LastVisitDate [1] KEY IDEA Use the built-in string and date functions: MID, LEFT and RIGHT to cut out text, STR_TO_NUM to convert, and TODAY, MONTH, DAYINDEX and SETDATE for dates. TOTAL 18 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Awab Aqib. Tap the picture to open it on YouTube.