How would you solve this?

Mar 21, 2013 at 3:54pm
If you had a bunch of test score and you were write a code to add a specific persons total score, any suggestions of how to do that?

For example:

sam 150
mary 200

sam 350
mary 100

sam 450
mary 2300

sam 180
mary 500

sam 10
mary 20

sam 10
mary 400

sam 600
mary 600
Last edited on Mar 21, 2013 at 4:13pm
Mar 21, 2013 at 4:28pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <map>
#include <string>
#include <iostream>
#include <fstream>

int main()
{
std::map<std::string, int> scores;
std::ifstream input("Somefile");
std::string name;
int score;
while(input >> name >> score)
    scores[name] += score;
for(auto v: scores)
    std::cout << "Total score of " << v.first << " is " << v.second << std::endl;
return 0;
}
Last edited on Mar 21, 2013 at 4:30pm
Mar 21, 2013 at 4:35pm
Thanks for the reply. What if the scores were randomly generated, would you use the same format?
Mar 21, 2013 at 4:40pm
Yes, for example:
1
2
3
4
for(int tests = 0; tests < 100; ++tests) {
    scores["Mike"] += random(101);
    scores["Jane"] += random(101);
}
Last edited on Mar 21, 2013 at 4:41pm
Topic archived. No new replies allowed.