Cambridge AS Level Computer Science 9618 · 10.3 Files

Q49 · 9618/21/O/N/22 Q8

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

Q499618/21/O/N/22original Q8Structured · 19 marks
Question 8 from 9618/21/O/N/22

Worked solution

(a)

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
ENDFUNCTION

Function 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]

(b)

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]

(c)

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
ENDPROCEDURE

Procedure 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]

(d)

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]

KEY IDEARead a file with a loop that tests EOF, count with a variable, and reuse modules already written such as CheckFile and CheckLine.
TOTAL 19 marks

Official mark scheme

Mark scheme for 9618/21/O/N/22 question 8
VIDEO
▶ 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.