Cambridge AS Level Computer Science 9618 · 11.3 Structured Programming

Q83 · 9618/21/O/N/23 Q6

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

Q839618/21/O/N/23original Q6Structured · 9 marks
Question 6 from 9618/21/O/N/23

Worked solution

(a)

Join the new string on, but output first if it would not fit, or if the Boolean is TRUE.

PROCEDURE MyOutput(NewString : STRING, EOL : BOOLEAN)
    IF LENGTH(MyString) + LENGTH(NewString) > 255 THEN
        OUTPUT MyString
        MyString ← NewString
    ELSE
        MyString ← MyString & NewString
    ENDIF
    IF EOL = TRUE THEN
        OUTPUT MyString
        MyString ← ""
    ENDIF
ENDPROCEDURE

Procedure heading with both parameters, and ending [1]

Concatenated string produced [1]

Check whether the resulting string would be longer than 255 characters [1]

If so, output the old MyString [1]

... and put NewString into MyString [1]

Otherwise concatenate NewString onto MyString [1]

Test the Boolean: if TRUE output MyString ... [1]

... and reset MyString to an empty string (also scores, but only seven marks are available).

(b)

Write: a new MyString is created each time the procedure is called [1]

Write: so the previous contents are lost and the string never builds up [1]

KEY IDEAA global variable keeps its value between calls, but a local variable starts again each time.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/21/O/N/23 question 6
VIDEO
▶ Starts at Q6 (37:37)

Starts at Q6 (37:37) · by Pseudocode Pro. 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.