Cambridge AS Level Computer Science 9618 · 11.1 Programming Basics

Q36 · 9618/23/O/N/25 Q4

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

Q369618/23/O/N/25original Q4Structured · 6 marks
Question 4 from 9618/23/O/N/25

Worked solution

(a)

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
ENDPROCEDURE

All 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.

KEY IDEATake the string three characters at a time, jumping 4 each time to skip the comma, and stop at the end of the string or when the array is full.
TOTAL 6 marks

Official mark scheme

Mark scheme for 9618/23/O/N/25 question 4
VIDEO
▶ 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.