Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Read the file line by line, count the non-blank lines and stop as soon as the count reaches 10.
FUNCTION CheckFile(ThisFile : STRING) RETURNS BOOLEAN
DECLARE Valid : BOOLEAN
DECLARE ThisLine : STRING
DECLARE Count : INTEGER
Count ← 0
Valid ← FALSE
OPEN ThisFile FOR READ
WHILE NOT EOF(ThisFile) AND Valid = FALSE
READFILE ThisFile, ThisLine
IF ThisLine <> "" THEN
Count ← Count + 1
IF Count = 10 THEN
Valid ← TRUE
ENDIF
ENDIF
ENDWHILE
CLOSEFILE ThisFile
RETURN Valid
ENDFUNCTIONFunction heading and ending with the parameter and the return type [1]
Integer Count declared and set to 0 [1]
File opened in READ mode and closed [1]
Conditional loop that runs until the end of the file (EOF) [1]
Reads a line in the loop, and adds 1 to Count if the line is not blank [1]
Loop stops once 10 non-blank lines have been read [1]
Returns a BOOLEAN value correctly in both cases [1]
Write the call with the file name and the maximum number of errors:
CALL CountErrors("Jim01Prog.txt", 20)Correct module name, with brackets and at least one correct parameter [1]
Completely correct statement, with CALL, the file name as a string and 20 [1]
First test the file with CheckFile(). If it is valid, read each line, call CheckLine() and count the non-zero results. Stop early when the count goes over the maximum.
PROCEDURE CountErrors(ThisFile : STRING, MaxErrors : INTEGER)
DECLARE ErrCount, ThisError : INTEGER
DECLARE ThisLine : STRING
ErrCount ← 0
IF CheckFile(ThisFile) = FALSE THEN
OUTPUT "That project file is not valid"
ELSE
OPEN ThisFile FOR READ
WHILE NOT EOF(ThisFile) AND ErrCount <= MaxErrors
READFILE ThisFile, ThisLine
ThisError ← CheckLine(ThisLine)
IF ThisError <> 0 THEN
ErrCount ← ErrCount + 1
ENDIF
ENDWHILE
CLOSEFILE ThisFile
IF ErrCount > MaxErrors THEN
OUTPUT "Warning: too many errors"
ELSE
OUTPUT "Number of errors: ", ErrCount
ENDIF
ENDIF
ENDPROCEDUREProcedure heading and ending with both parameters [1]
Integer ErrCount declared and set to 0 [1]
Uses CheckFile(), outputs an error message and ends if it returns FALSE [1]
Conditional loop that runs until the end of the file [1]
Loop also stops when ErrCount is greater than MaxErrors [1]
Reads a line and uses it as the parameter of CheckLine(), inside the loop [1]
Tests the returned value and adds 1 to ErrCount if it is not zero [1]
Outputs the correct message once only (number of errors, or the warning) [1]
Write any two:
Incorrect block structure, for example a missing ENDPROCEDURE, ENDFUNCTION or ENDTYPE [1]
A data type error, for example putting an integer into a string; an identifier used before it is declared; or incorrect parameter use [1]
▶ Whole paper
Video solution of the whole paper · 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.