Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 3 of 3.
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
ENDPROCEDUREProcedure 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).
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]
▶ 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.