Procedures, Functions, Arrays and File Handling: Question 5
Syllabus 8.3
A program stores the high scores from a simple game, held in the array Scores, in a text file called scores.txt, with one score written per line. Scores contains 4 values: Scores[1] ← 82, Scores[2] ← 95, Scores[3] ← 78, Scores[4] ← 90.
(a) Complete the pseudocode below, which opens the file for writing, writes all 4 values from Scores to the file, one per line, and then closes the file. Write the missing pseudocode statement for each of the lines labelled (i), (ii) and (iii).
DECLARE Index : INTEGER
_____(i)_____
FOR Index ← 1 TO 4
_____(ii)_____
NEXT Index
_____(iii)_____
[3]
(b) The file scores.txt now contains four lines: 82, 95, 78, 90. A separate procedure reads every score back from the file and adds it to a running total, using the EOF function so that it works correctly whatever number of scores the file contains.
DECLARE FileScore : INTEGER
DECLARE RunningTotal : INTEGER
RunningTotal ← 0
OPENFILE "scores.txt" FOR READ
WHILE NOT EOF("scores.txt")
READFILE "scores.txt", FileScore
RunningTotal ← RunningTotal + FileScore
ENDWHILE
CLOSEFILE "scores.txt"
OUTPUT RunningTotal
Trace this code and state the value output for RunningTotal. [2]
(c) State one reason why the procedure in (b) uses WHILE NOT EOF("scores.txt") to control the loop, rather than a fixed count such as FOR Index ← 1 TO 4. [1]
Show worked solution Hide worked solution
Worked solution
Part (a): Completing the pseudocode that writes the array to a file
To write data to a file, the file must first be opened in write mode:
(i) OPENFILE "scores.txt" FOR WRITE
Inside the loop, one element of the array must be written to the file on each pass, so the loop
writes Scores[Index] for Index from 1 to 4, giving one score per line:
(ii) WRITEFILE "scores.txt", Scores[Index]
Once every value has been written, the file must be closed so the data is properly saved and the file is released:
(iii) CLOSEFILE "scores.txt"
Part (b): Tracing the WHILE NOT EOF loop that reads the file back
RunningTotal starts at 0. The loop reads one line into FileScore and adds it to
RunningTotal on every pass, continuing until EOF("scores.txt") becomes TRUE (i.e. there is no
more data left to read):
| Read | FileScore | RunningTotal after this step |
|---|---|---|
| 1st | 82 | 0 + 82 = 82 |
| 2nd | 95 | 82 + 95 = 177 |
| 3rd | 78 | 177 + 78 = 255 |
| 4th | 90 | 255 + 90 = 345 |
After the 4th read, EOF("scores.txt") becomes TRUE, so the loop stops. CLOSEFILE then closes
the file, and OUTPUT RunningTotal displays 345.
Part (c): Why use EOF instead of a fixed count
A fixed-count loop such as FOR Index ← 1 TO 4 only works correctly if the file happens to
contain exactly 4 lines. If the game later stored a fifth high score, that score would never be
read, and if the file contained fewer than 4 scores, the program would try to read past the end of
the file. Using WHILE NOT EOF("scores.txt") instead means the loop automatically keeps reading
one line at a time until it genuinely reaches the end of the file, so the same procedure works
correctly regardless of how many scores the file actually contains.
Final answers
- (a) (i)
OPENFILE "scores.txt" FOR WRITE(ii)WRITEFILE "scores.txt", Scores[Index](iii)CLOSEFILE "scores.txt" - (b) RunningTotal = 345
- (c) EOF lets the loop keep reading until it reaches the true end of the file, so it works for any number of stored scores without that number being hard-coded.