9618/23/M/J/23 question 4: worked solution (May/June 2023, paper 23)
Show as plain textHide 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. Q82 9618/23/M/J/23 original Q4 Structured · 6 marks Worked solution Check the count first. If it is valid, build the string in a loop by joining the character on each time. FUNCTION MakeString(Count : INTEGER, AChar : CHAR) RETURNS STRING DECLARE MyString : STRING DECLARE Index : INTEGER IF Count < 1 THEN MyString ← "ERROR" ELSE MyString ← "" FOR Index ← 1 TO Count MyString ← MyString & AChar NEXT Index ENDIF RETURN MyString ENDFUNCTION Function heading and ending with both parameters and the return type [1] Local variables Index and MyString declared [1] Test for Count < 1 and, if true, give "ERROR" (an immediate RETURN also scores) [1] Loop that runs Count times [1] Concatenation used to add the character, with the string set to "" before the loop [1] The string is returned correctly in both cases [1] KEY IDEA Test the invalid case first, then build a string by joining one character per loop pass. TOTAL 6 marks Official mark scheme VIDEO ▶ Starts at Q4 (52:30) Starts at Q4 (52:30) · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.