Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Think about what is lost when the computer is switched off: the data in the arrays. Write:
Before the program ends (at the end of the day), the data in the arrays is written to text files [1]
At the start of the next day the data is read from the files back into the arrays, so it does not have to be typed in again [1]
Write: the data in a text file is kept when the computer is switched off, because the file is non-volatile (permanent) storage [1]
Join the three items into one string and store it as one line of the file. Then separate the items:
Join the name, number of people and event type into one string, stored as a single line in the file [1]
Put a special separator character between the items (or give each item a fixed length) so they can be split apart again [1]
Another accepted way: turn the number of people into a string and store each item on its own line, one after the other.
Open the file, check if it is empty, then read and output up to five lines with a conditional loop:
PROCEDURE Preview(ThisFile : STRING)
DECLARE LineNum : INTEGER
DECLARE ThisLine : STRING
OPENFILE ThisFile FOR READ
IF EOF(ThisFile) THEN
OUTPUT "Warning: the file is empty"
ELSE
LineNum ← 1
WHILE LineNum < 6 AND NOT EOF(ThisFile)
READFILE ThisFile, ThisLine
OUTPUT ThisLine
LineNum ← LineNum + 1
ENDWHILE
ENDIF
CLOSEFILE ThisFile
ENDPROCEDUREProcedure heading with the file name parameter, and ENDPROCEDURE [1]
File opened for reading and closed afterwards [1]
Checks if the file is empty and outputs a warning message if it is [1]
Conditional loop that stops after five lines or at the end of the file [1]
Inside the loop, outputs the line and reads the next one [1]
▶ 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.