9618/21/M/J/21 question 7: worked solution (May/June 2021, paper 21)

Show as plain text

Cambridge AS Level Computer Science 9618 · 11.3 Structured Programming Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 3 of 3. Q65 9618/21/M/J/21 original Q7 Structured · 16 marks 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 IDEA Words 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 VIDEO ▶ Whole paper Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.