Paper 2 scenario practice
File-based score statistics
The scenario
A teacher has saved student test scores in a text file called "scores.txt". Each line of the file contains ONE score (an integer between 0 and 100). The teacher wants a program that:
1. OPENS "scores.txt" for reading.
2. Reads each line until the END OF FILE, accumulating: the COUNT of scores, the TOTAL of all scores, and the MAXIMUM score seen.
3. CLOSES "scores.txt".
4. Computes the AVERAGE = total / count (only if count > 0).
5. OPENS a different file "summary.txt" for writing.
6. WRITES three lines to it: the count, the average, and the maximum.
7. CLOSES "summary.txt".
8. If the input file was empty (count = 0), output a message instead of writing the summary file.
Use Cambridge file-handling commands: OPENFILE, READFILE, WRITEFILE, CLOSEFILE, and the EOF() function.
Write a program in pseudocode (or a high-level language of your choice). Marks will be awarded for correct file open/close, EOF-controlled reading, accumulating statistics, and writing to the output file.
Marking scheme — total 15 marks
Cambridge IGCSE 0478 Paper 2
- 2Open input file and initialise totals: OPENFILE "scores.txt" FOR READ. DECLARE the variables (score, count, total, max) and initialise count ← 0, total ← 0, max ← -1 (or 0) so the first comparison sets it correctly.
- 4EOF-controlled read loop: WHILE NOT EOF("scores.txt"): READFILE "scores.txt", score. Accumulate count and total. IF score > max THEN max ← score. The loop stops when EOF is TRUE.
- 2Close input and compute average: CLOSEFILE "scores.txt" after the loop. IF count > 0 THEN average ← total / count — guards against divide-by-zero.
- 2Empty-file fallback: IF count = 0 THEN OUTPUT a message saying the file was empty AND skip writing the summary file. The empty case must not crash the program.
- 3Write summary file: ELSE branch: OPENFILE "summary.txt" FOR WRITE. WRITEFILE three lines (count, average, max). CLOSEFILE "summary.txt". File must be opened and closed correctly.
- 2Program structure and naming: WHILE/ENDWHILE properly paired; IF/ENDIF visible; clear identifiers (count, total, max — not c, t, m).
Hints — reveal one at a time, only if stuck
Your pseudocode workspace
1 line