Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Choose a character that is not in the data, for example #:
Character: any symbol that is not a letter, number, comma, colon or space, for example # [1]
Reason: the character never appears in the data being recorded, so it cannot be confused with the data [1]
Write: design [1]
The Student ID is the first six characters of each element. Loop through all 2000 elements, open LogFile for append, write matching entries, count them and clear them:
FUNCTION LogEvents(StudentID : STRING) RETURNS INTEGER
DECLARE FileData : STRING
DECLARE Index, Count : INTEGER
CONSTANT LogFile = "LogFile"
Count ← 0
OPENFILE LogFile FOR APPEND
FOR Index ← 1 TO 2000
FileData ← LogArray[Index]
IF LEFT(FileData, 6) = StudentID THEN
WRITEFILE LogFile, FileData
Count ← Count + 1
LogArray[Index] ← ""
ENDIF
NEXT Index
CLOSEFILE LogFile
RETURN Count
ENDFUNCTIONFunction heading with the parameter and return type, and ENDFUNCTION [1]
LogFile opened for APPEND and closed afterwards [1]
Loop that runs 2000 times [1]
First 6 characters taken from the array element, inside the loop [1]
First 6 characters compared with the parameter [1]
If equal: whole element written to the file, Count increased and the element set to an empty string [1]
Count returned (it was declared and set to 0) [1]
▶ Whole paper
Video solution of the whole paper · by Mr Saem. 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.