Procedures, Functions, Arrays and File Handling: Question 10
Syllabus 8.2, 8.3
A small sports club stores its members' names in a text file, register.txt, one name per line, so the membership list survives between seasons. The pseudocode below reads every name from the file into a one-dimensional array, Names, declared with room for up to 10 names, and counts how many names were actually read.
DECLARE Names : ARRAY[1:10] OF STRING
DECLARE NameCount : INTEGER
DECLARE NextName : STRING
NameCount ← 0
OPENFILE "register.txt" FOR READ
WHILE NOT EOF("register.txt")
READFILE "register.txt", NextName
NameCount ← NameCount + 1
Names[NameCount] ← NextName
ENDWHILE
CLOSEFILE "register.txt"
OUTPUT NameCount
register.txt currently contains exactly four lines, in this order: Amara, Beth, Chidi, Dev.
(a) State the value output for NameCount after this code runs. [1]
(b) State the value of Names[3] after this code runs. [1]
(c) State what Names[5] to Names[10] hold after this code runs, and explain why. [2]
(d) A club administrator wants to add a fifth member, Esi, to the end of register.txt without
erasing the four names already stored there. State the file mode that OPENFILE should use for
this, and explain why OPENFILE "register.txt" FOR WRITE would not achieve this. [2]
Show worked solution Hide worked solution
Worked solution
Part (a): Tracing NameCount
The loop runs once for every line in register.txt, incrementing NameCount and storing that
name in Names[NameCount] each time, until EOF("register.txt") becomes TRUE:
| Read | NextName | NameCount after this step |
|---|---|---|
| 1st | Amara | 1 |
| 2nd | Beth | 2 |
| 3rd | Chidi | 3 |
| 4th | Dev | 4 |
After the 4th read, EOF("register.txt") becomes TRUE, so the loop stops. OUTPUT NameCount
displays 4. [1 mark]
Part (b): Reading a single array element
From the trace above, Names[3] is assigned during the 3rd read, when NextName = "Chidi". So
Names[3] = "Chidi". [1 mark]
Part (c): What the unused array elements hold
Names was declared with room for 10 names, ARRAY[1:10] OF STRING, but the loop only ever
assigns to Names[1] through Names[4], because NameCount stops increasing once all 4 lines
of register.txt have been read and EOF("register.txt") becomes TRUE. The statement
Names[NameCount] ← NextName is therefore never executed with NameCount equal to 5, 6, 7, 8, 9
or 10.
As a result, Names[5] to Names[10] are never given a value by this code at all, they hold no
defined data, they are simply the unused remainder of the array’s declared capacity. [2
marks]: [1] for recognising these elements are never assigned, [1] for the correct
reason (NameCount only reaches 4, matching the 4 lines in the file).
Part (d): Adding a new name without erasing the existing register
To add Esi to the end of register.txt while keeping Amara, Beth, Chidi and Dev intact, the file
must be opened with:
OPENFILE "register.txt" FOR APPEND
OPENFILE "register.txt" FOR WRITE would not work here, because opening a file in write mode
creates a fresh, empty file (erasing any data already stored under that filename) rather than
adding new data to what is already there. If FOR WRITE were used, the four existing names would
be lost the moment the file was opened, before Esi was even written. FOR APPEND, by contrast,
opens the existing file and positions any newly written data after its current content, so the
four existing names are preserved and Esi is simply added after them. [2 marks]: [1] for
stating FOR APPEND, [1] for correctly explaining why FOR WRITE would erase the existing
data.
Final answers
- (a) NameCount = 4
- (b) Names[3] = “Chidi”
- (c) Names[5] to Names[10] hold no defined value, they are never assigned by this code
- (d) Use
OPENFILE "register.txt" FOR APPEND;FOR WRITEwould erase the four existing names