9618/23/M/J/22 question 7: worked solution (May/June 2022, paper 23)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 12.3 Program Testing and Maintenance Worked solution from the Software Development Paper 2 Problem-solving and Programming booklet, Part 3 of 3. Q39 9618/23/M/J/22 original Q7 Structured · 8 marks Worked solution (a)(i) The lines are output in the wrong order , not as they appear in the file. [1] (a)(ii) Description: The last line of the file is not always stored in Buffer [3] . [1] So the FOR loop that outputs Buffer [1] to Buffer [3] gives the lines in the wrong sequence. [1] Explanation (any two of these): Use a shuffle: copy Buffer [2] to Buffer [1] and Buffer [3] to Buffer [2] [1] Then read the next line from the file and store it in Buffer [3] [1] Another way: remember the index of the last line stored, and output starting after it, wrapping round using MOD 3. Or read the file once to count the lines, close and reopen it, discard lines up to the count minus 3, then read and output the last three. (b) The correct output happens when the number of lines in the file is a multiple of three . [1] (c) Use MOD to wrap the counter from 3 back to 1: LineNum ← (LineNum MOD 3) + 1 An assignment to LineNum that uses MOD [1] The statement is completely correct [1] ((LineNum + 3) MOD 3) + 1 is also accepted. KEY IDEA A circular buffer must wrap its index with MOD, and the output must start from the oldest line, not always from element 1. TOTAL 8 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Digify ID. Tap the picture to open it on YouTube.