@Zarman I don't think you read the question properly. It doesn't ask you to average all the values, but instead the average for each student (there are different students in the files).
Plus simply posting code with no explanation or without properly testing, doesn't help anybody.
@jakobfri, can we see what you have? You said you already had part of the code to sort students names, and if you did it properly this shouldn't be too hard to modify to accept numerical inputs as well.
Also perhaps some more details on the rules of the file would be helpful in providing an efficient answer as opposed to a more general answer.
For example, if the file is in a strict form of:
[Student A] [grade]
[Student A] [grade]
[Student A] [grade]
...
[Student B] [grade]
[Student B] [grade]
[Student B] [grade]
...
|
and the student information is not mixed it is possible to use two sets of vectors one containing the student name, and the other containing the average which is calculated as you read each sequential line in the file.
However if the file follows this format:
[Student A] [grade]
[Student B] [grade]
[Student A] [grade]
[Student C] [grade]
[Student B] [grade]
...
|
And there is no apparent order in the way students and their grade data is contained in the file then you can use a map, which uses the name of the student as the key and a vector or some other type of container containing the list of grades for that student, you could also keep a vector of the names to keep track of the keys (aka names). At the end of the file just run through the map using the vector of keys, and calculate the average for each student.
This however, can be improved in terms of efficiency, and you can try and see how exactly to improve the efficiency of this method.