Cambridge AS Level Computer Science 9618 · 11.3 Structured Programming

Q82 · 9618/23/M/J/23 Q4

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

Q829618/23/M/J/23original Q4Structured · 6 marks
Question 4 from 9618/23/M/J/23

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 IDEATest the invalid case first, then build a string by joining one character per loop pass.
TOTAL 6 marks

Official mark scheme

Mark scheme for 9618/23/M/J/23 question 4
VIDEO
▶ Starts at Q4 (52:30)

Starts at Q4 (52:30) · 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.