9618/22/O/N/21 question 5: worked solution (October/November 2021, paper 22)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 10.3 Files Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4. Q45 9618/22/O/N/21 original Q5 Structured · 10 marks Worked solution (a) 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 ENDPROCEDURE Procedure 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] (b)(i) Write: store all three items on one line (for example separated by a special character). [1] (b)(ii) 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] KEY IDEA Read a whole employee record (three lines) in each loop, count every record, and write and count only those that pass the test. TOTAL 10 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by CS BY AQIB KHAN. Tap the picture to open it on YouTube.