9618/21/O/N/24 question 3: worked solution (October/November 2024, paper 21)
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. Q61 9618/21/O/N/24 original Q3 Structured · 9 marks Worked solution (a) Fill each gap: 1: Open the file Result.txt in read mode [1] 2: Loop until EOF(Result.txt) (the end of the file) [1] 3: Read the next line from the file and store in ThisLine [1] 7: After the loop, close the file [1] (b) Write: steps 4, 5 and 6 keep the last three lines in the correct order , with each new line moved along (the same order as in the file) [1] (c) Two changes are needed. Either method scores: Loop until the given line number minus 1 lines have been read, and return FALSE if the end of file is reached first [1] Then loop three times , reading one line each time and outputting it [1] Return FALSE if the end of file is reached while reading these lines [1] After the three lines are output, return TRUE [1] Other accepted points: the steps that move LineX, LineY and LineZ along are no longer needed; or one loop can read up to the given line number + 2 (or EOF), keeping steps 4 to 6 and returning FALSE if EOF is reached. KEY IDEA To keep the last few lines of a file, shift each line along as a new one is read, then use the stored lines after the loop. TOTAL 9 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Pseudocode Pro. Tap the picture to open it on YouTube.