Cambridge AS Level Computer Science 9618 · 10.3 Files

Q46 · 9618/21/M/J/22 Q5

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.

Q469618/21/M/J/22original Q5Structured · 6 marks
Question 5 from 9618/21/M/J/22

Worked solution

Describe the algorithm

Describe the steps in words, with no pseudocode.

Any six of these points:

Open the file in read mode [1]

Set up a conditional loop that repeats until the value is found or the end of the file (EOF) is reached [1]

Read a line from the file inside the loop [1]

Extract Field 2 from the line [1]

Compare the extracted field with the search value [1]

If it matches, extract Field 1 and Field 3 and output them [1]

Other points that also score: say how Field 2 is extracted, for example with a substring function using the lengths of Field 1 and Field 2; close the file after the loop has finished.

KEY IDEARead line by line, cut out the fixed-length field with a substring, compare it, and stop at the first match.
TOTAL 6 marks

Official mark scheme

Mark scheme for 9618/21/M/J/22 question 5
VIDEO
▶ 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.