Cambridge AS Level Computer Science 9618 · 11.3 Structured Programming

Q65 · 9618/21/M/J/21 Q7

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

Q659618/21/M/J/21original Q7Structured · 16 marks
Question 7 from 9618/21/M/J/21

Worked solution

(a)

Count spaces to find where the word starts. Word 1 always starts at position 1.

FUNCTION GetStart(WordNum : INTEGER) RETURNS INTEGER
    DECLARE Index, ThisPos, NumFound : INTEGER
    DECLARE ThisChar : CHAR
    CONSTANT SPACECHAR = ' '
    Index ← -1
    NumFound ← 0
    ThisPos ← 1
    IF WordNum = 1 THEN
        Index ← 1
    ELSE
        WHILE ThisPos <= LENGTH(FNString) AND Index = -1
            ThisChar ← MID(FNString, ThisPos, 1)
            IF ThisChar = SPACECHAR THEN
                NumFound ← NumFound + 1
                IF NumFound = WordNum - 1 THEN
                    Index ← ThisPos + 1
                ENDIF
            ENDIF
            ThisPos ← ThisPos + 1
        ENDWHILE
    ENDIF
    RETURN Index
ENDFUNCTION

Function heading with return type, and ENDFUNCTION [1]

Loop that counts spaces until the word is found or the end of FNString is reached [1]

Extract one character from FNString inside the loop [1]

Compare it with the space character and add 1 to the count if equal [1]

Compare the count with WordNum - 1 inside the loop [1]

If equal, set Index (or a flag) to ThisPos + 1 [1]

Return Index in all cases [1]

Word 1 handled as a special case (starts at position 1) also earns a mark, but the part is capped at 7. Other correct methods also score.

(b)

Describe the steps in order:

Use a variable as an array index [1]

Use a loop to check through the IgnoreList array [1]

If the word equals the current element, return FALSE (or set a flag) [1]

If the word is not already there, find an unused (empty) element, store the word and return TRUE; if there is no unused element, return FALSE [1]

Another accepted order: save the index of the first unused element while looping, then store the word there at the end.

(c)

Build the word one character at a time until a space or the end of the string.

FUNCTION GetWord(Index : INTEGER) RETURNS STRING
    DECLARE NextWord : STRING
    DECLARE Done : BOOLEAN
    DECLARE ThisChar : CHAR
    CONSTANT SPACECHAR = ' '
    NextWord ← ""
    Done ← FALSE
    REPEAT
        ThisChar ← MID(FNString, Index, 1)
        IF ThisChar <> SPACECHAR THEN
            NextWord ← NextWord & ThisChar
        ENDIF
        IF ThisChar = SPACECHAR OR Index = LENGTH(FNString) THEN
            Done ← TRUE
        ENDIF
        Index ← Index + 1
    UNTIL Done = TRUE
    RETURN NextWord
ENDFUNCTION

A conditional loop (REPEAT or WHILE) [1]

Extract a character from FNString and compare it with the space character, inside the loop [1]

Add the character to NextWord if it is not a space [1]

Exit the loop at a space or at the end of FNString [1]

Return NextWord (it must start as an empty string) [1]

Another accepted way: find the end position in a loop, then use MID(FNString, Index, length) and return it.

KEY IDEAWords are separated by single spaces, so count spaces to find a word start and read characters until the next space.
TOTAL 16 marks

Official mark scheme

Mark scheme for 9618/21/M/J/21 question 7
VIDEO
▶ Whole paper

Video solution of the whole paper · by Papersdock. 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.