9618/23/O/N/23 question 6: worked solution (October/November 2023, paper 23)

Show as 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. Q18 9618/23/O/N/23 original Q6 Structured · 6 marks Worked solution (a) Test the highest return value first, so the highest possible value is returned. Use LEFT, RIGHT and MID to pick out the digits: FUNCTION TestNum(ThisNum : STRING) RETURNS INTEGER IF LEFT(ThisNum, 3) = RIGHT(ThisNum, 3) THEN RETURN 3 ENDIF IF RIGHT(ThisNum, 3) = "000" THEN RETURN 2 ENDIF IF MID(ThisNum, 4, 1) = MID(ThisNum, 5, 1) AND MID(ThisNum, 5, 1) = MID(ThisNum, 6, 1) THEN RETURN 1 ENDIF RETURN 0 ENDFUNCTION Function heading and ending with the STRING parameter and INTEGER return type [1] Test for return value 3: first three digits equal last three digits [1] Test for return value 2: last three digits are "000" [1] Test for return value 1: last three digits all the same [1] The highest value is returned when more than one condition is true (3 is tested before 2 before 1) [1] Zero returned when no condition is met [1] KEY IDEA Test the conditions from the highest return value down, and return 0 at the end. TOTAL 6 marks Official mark scheme VIDEO ▶ Starts at Q6 (1:33:40) Starts at Q6 (1:33:40) · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.