Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Open the old file to read and the new file to write. Read the lines three at a time, because one employee is three lines. Count each set, and copy it only if the status is not the search value.
PROCEDURE MakeNewFile(OldFile, NewFile, Status : STRING)
DECLARE Line1, Line2, Line3 : STRING
DECLARE NumRecs, NumCopied : INTEGER
NumRecs ← 0
NumCopied ← 0
OPENFILE OldFile FOR READ
OPENFILE NewFile FOR WRITE
WHILE NOT EOF(OldFile)
READFILE OldFile, Line1
READFILE OldFile, Line2
READFILE OldFile, Line3
NumRecs ← NumRecs + 1
IF Line3 <> Status THEN
WRITEFILE NewFile, Line1
WRITEFILE NewFile, Line2
WRITEFILE NewFile, Line3
NumCopied ← NumCopied + 1
ENDIF
ENDWHILE
CLOSEFILE OldFile
CLOSEFILE NewFile
OUTPUT "File ", OldFile, " contained ", NumRecs, " employee details"
OUTPUT NumCopied, " employee sets of details were written to file ", NewFile
ENDPROCEDUREProcedure heading and ending, with the three parameters [1]
OldFile opened for READ, NewFile opened for WRITE, and both closed afterwards [1]
Conditional loop that runs until EOF(OldFile) [1]
Three lines read from OldFile inside the loop [1]
Third line compared with the Status parameter, and if not equal all three lines written to NewFile [1]
Both counts declared and set to 0, then counted inside the loop [1]
Final output after the loop with both counts, both file names and suitable text [1]
Write: store all three items on one line (for example separated by a special character). [1]
Write one advantage and one disadvantage.
Advantage: fewer file operations are needed (one read or write per employee instead of three) [1]
Disadvantage: the algorithm to combine or extract the separate items from a line is more complex [1]
▶ Whole paper
Video solution of the whole paper · by CS BY AQIB KHAN. 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.