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