Advanced Algorithms and Recursion: Question 10

Syllabus 20.2

Structured A2 8 marks

A programmer writes the following pseudocode to count how many records are stored in a text file, using exception handling in case the file cannot be found.

DECLARE StudentRecord : STRING
DECLARE Total : INTEGER
Total ← 0

TRY
    OPENFILE "Scores.txt" FOR READ
    WHILE NOT EOF("Scores.txt")
        READFILE "Scores.txt", StudentRecord
        Total ← Total + 1
    ENDWHILE
    CLOSEFILE "Scores.txt"
    OUTPUT "Records read", Total
CATCH FileNotFound
    OUTPUT "Error, file could not be opened"
ENDTRY

(a) Explain the purpose of EOF("Scores.txt") in the WHILE loop's condition, and state what would be likely to happen if the loop instead used WHILE TRUE DO with no EOF check. [2]

(b) Explain the purpose of the TRY ... CATCH ... ENDTRY structure in this pseudocode, describing what the program does if "Scores.txt" does not exist, compared with what would be likely to happen without exception handling. [3]

(c) "Scores.txt" exists and contains 5 records. State the value of Total output by this program, explaining your reasoning; then state what would instead be output if "Scores.txt" did not exist. [3]

Show worked solution Hide worked solution

Worked solution

Part (a): The role of EOF in the WHILE loop

EOF("Scores.txt") (End Of File) returns TRUE once the file’s read position has reached the end of the file, and FALSE while there are still unread records. NOT EOF("Scores.txt") is therefore TRUE for as long as more records remain to be read, and becomes FALSE exactly when the last record has been read. This condition is re-tested every time control returns to the top of the loop, immediately after each READFILE.

If the loop instead used WHILE TRUE DO with no EOF check, the loop would never test whether the end of the file had been reached. Once all 5 records had been read, READFILE would still be called again on the next iteration, attempting to read past the end of the file. This would be likely to cause a runtime error, rather than the loop ending cleanly once every record has been counted.

[2 marks]: [1] for correctly explaining what EOF tests and why it controls the loop, [1] for correctly explaining the likely consequence (a runtime error from reading past the end of the file) of omitting it.

Part (b): The role of TRY… CATCH… ENDTRY

The TRY block contains the code the program attempts to run under normal conditions: opening “Scores.txt”, reading its records, and closing it again. If OPENFILE "Scores.txt" FOR READ fails, because the file does not exist, this raises a FileNotFound exception, and the program immediately jumps to the matching CATCH FileNotFound block instead of continuing with the rest of the TRY block (so WHILE NOT EOF(...), CLOSEFILE, and the “Records read” output are all skipped). The CATCH block then outputs an error message, letting the program continue running in a controlled way.

Without this exception handling, attempting to open a file that does not exist would be likely to cause the program to terminate abruptly with an unhandled runtime error, rather than detecting the problem and reporting it gracefully to the user.

[3 marks]: [1] for explaining that TRY attempts the file-handling code, [1] for explaining that a missing file causes control to jump to CATCH instead of continuing the TRY block, [1] for correctly contrasting this with the likely uncontrolled crash that would occur without exception handling.

Part (c): Tracing Total for both cases

If “Scores.txt” exists and holds 5 records: Total starts at 0. Each pass through the WHILE loop reads one record with READFILE and executes Total ← Total + 1, so Total increases by 1 for each of the 5 records: 0 → 1 → 2 → 3 → 4 → 5. EOF("Scores.txt") only becomes TRUE once the fifth (last) record has been read, so the loop then stops having run exactly 5 times. CLOSEFILE runs, and OUTPUT "Records read", Total outputs Total = 5.

If “Scores.txt” does not exist: OPENFILE fails, raising the FileNotFound exception, so none of the code inside the rest of the TRY block runs. Total remains at its initialised value of 0, but this is never output, since OUTPUT "Records read", Total is also skipped. The CATCH block instead outputs "Error, file could not be opened".

[3 marks]: [1] for the correct value Total = 5 when the file exists, with valid reasoning, [1] for correctly identifying that the loop runs exactly 5 times because EOF becomes true after the fifth record, [1] for correctly stating that the error message (and not a value of Total) is output when the file does not exist.

Final answers

  • (a) EOF tests whether the end of the file has been reached, stopping the loop once all records are read; omitting it would be likely to cause a runtime error from reading past the last record.
  • (b) TRY attempts the file-handling code; a missing file raises FileNotFound and jumps to CATCH, which reports the error instead of the program crashing.
  • (c) If the file exists: Total = 5 is output. If it does not exist: "Error, file could not be opened" is output instead, and Total is never output.