Cambridge AS Level Computer Science 9618 · 10.3 Files

Q44 · 9618/21/O/N/21 Q5

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

Q449618/21/O/N/21original Q5Structured · 10 marks
Question 5 from 9618/21/O/N/21

Worked solution

(a)

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]

(b)

Write: design [1]

(c)

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
ENDFUNCTION

Function 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]

KEY IDEABecause the Student ID is a fixed six characters, LEFT(entry, 6) picks it out; open in APPEND mode so old log lines are kept.
TOTAL 10 marks

Official mark scheme

Mark scheme for 9618/21/O/N/21 question 5
VIDEO
▶ 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.