Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3.
Each number takes 3 characters plus a comma, so the next number starts 4 characters later. Stop when the end of the string is reached or the array is full (20 elements).
PROCEDURE Store()
DECLARE Index, StartPos, ThisNum : INTEGER
DECLARE ThisString : STRING
Index ← 1
StartPos ← 1
WHILE StartPos < LENGTH(NumString) AND Index <= 20
ThisString ← MID(NumString, StartPos, 3)
ThisNum ← STR_TO_NUM(ThisString)
Data[Index] ← ThisNum
Index ← Index + 1
StartPos ← StartPos + 4
ENDWHILE
ENDPROCEDUREAll local variables declared [1]
Conditional loop that stops at the end of the string [1]
... and when the array is full [1]
MID used to take the next three characters from the right place [1]
Substring converted to an integer and stored in the array element [1]
Index increased by 1 and StartPos by 4 inside the loop [1]
Other ways also score, for example a FOR loop to 20 with a test for the end of the string, or reading one character at a time and testing for a comma.
▶ Topic lesson
A lesson on this topic · by ZAINEMATICS. 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.