Cambridge AS Level Computer Science 9618 · 10.3 Files

Q55 · 9618/22/O/N/23 Q6

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

Q559618/22/O/N/23original Q6Structured · 9 marks
Question 6 from 9618/22/O/N/23

Worked solution

(a)

Loop for each file number, turn it into a three-character suffix by adding zeros at the front, then create the file and write one line to it.

PROCEDURE CreateFiles(NameRoot : STRING, NumFiles : INTEGER) DECLARE FileName, Suffix : STRING DECLARE Count : INTEGER FOR Count ← 1 TO NumFiles Suffix ← NUM_TO_STR(Count) WHILE LENGTH(Suffix) <> 3 Suffix ← '0' & Suffix ENDWHILE FileName ← NameRoot & '.' & Suffix OPENFILE FileName FOR WRITE WRITEFILE FileName, "This is File " & FileName CLOSEFILE FileName NEXT Count ENDPROCEDURE

Procedure heading with both parameters, and ending [1]

Loop that runs NumFiles times [1]

Attempt to make the suffix using NUM_TO_STR() inside a loop [1]

Completely correct file name, for example TestData.002 [1]

OPENFILE in WRITE mode and the matching CLOSEFILE, inside the loop [1]

The first line written to each file inside the loop [1]

(b)(i)

It returns a value, so use a function. [1]

(b)(ii)

FUNCTION CheckFiles(NameRoot : STRING) RETURNS INTEGER

Correct header with the string parameter and RETURNS INTEGER [1]

(b)(iii)

The files are only being checked and read, so use READ mode. [1]

KEY IDEABuild file names with string functions and always open and close each file inside the loop.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/22/O/N/23 question 6
VIDEO
▶ Starts at Q6 (33:35)

Starts at Q6 (33:35) · by Pseudocode Pro. 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.