Answer it
Average score from a CSV file
5 marksYou mark it yourself against the marking points
A swimming club keeps each swimmer's best time trial score in a file called scores.csv. Each line holds a name and a whole-number score, separated by a comma, for example Ana,12. This program prints the average score:
total = 0
count = 0
with open("scores.csv", "r") as f:
for line in f:
parts = line.strip().split(",")
total = total + int(parts[1])
count = count + 1
print(total / count)
int total = 0;
int count = 0;
BufferedReader reader = new BufferedReader(new FileReader("scores.csv"));
String line = reader.readLine();
while (line != null) {
String[] parts = line.split(",");
total = total + Integer.parseInt(parts[1]);
count = count + 1;
line = reader.readLine();
}
reader.close();
System.out.println((double) total / count);
(a) State why the score is passed to int (in Java, Integer.parseInt) before it is added to total. [1]
(b) The club secretary adds an empty line at the end of the file. Explain what happens when the program reaches that line. [2]
(c) Outline one change to the loop that would let the program cope with empty lines. [1]
(d) State one reason the file is closed once it has been read. [1]
[5]