Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
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]
It returns a value, so use a function. [1]
FUNCTION CheckFiles(NameRoot : STRING) RETURNS INTEGER
Correct header with the string parameter and RETURNS INTEGER [1]
The files are only being checked and read, so use READ mode. [1]
▶ 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.