Hi, i was looking on the practise question sticky and i came across this question,

Write a program that allows you to input students' scores and weights. The program should then calculate a weighted average and score based on the data inputted by the user. (Beginner)

I'm not going to ask you for the answer but i was wondering should i use a array to keep track of the multiple students?

Many thanks.

5
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by chiwawa10

use one array to store the score of each student and another to store the weight of each student. If you know the number of students beforehand then it's all the more easier.

The question doesn't say how many though :O

If you don't know the number of students, perhaps you could use a vector.
Just push every student in the vector.

But I thought that the example program was meant for only one student. But it's always a good idea to set yourself some challenges!

``````//new vector pari.first = student id, pair.second = vector with test score
vector<pair<int, vector<int> > > students;
int id = 0;
//add new student with id = 0
students.push_back(pair<int, vector<int> >(id, vector<int>()));
int test_score = 10;
//search for id and if find it add test score to pair.second vector
for (size_t i = 0; i < students.size(); ++i)
if (students[i].first == id)
students[i].second.push_back(test_score);``````

and map version

``````map<int, vector<int> > students;
map<int, vector<int> >::iterator it;
int id = 0;
//new student with id = 0 and empty vector
students[id] = vector<int>();
int test_score = 10;
//add new test score to vector
students.find(id)->second.push_back(test_score);``````

you can add many scores to vector :P

ivailosp: I don't think that this is what Black Magic asked for. Giving complete answers to people is also against the forum rules.
It's nice to see that you are enthusiastic in helping people, but handing out code to everyone doesn't help them understand C++ better.

Niek

Write a program that allows you to input students' scores and weights. The program should then calculate a weighted average and score based on the data inputted by the user.

my code do that? how come
my code only show 2 ways to use map and vector - how come showing how to use stl is wrong

I'm sorry but, i was wanting to do this by myself (the actual code) i was just really asking for what i should use. Thanks...

This wasn't even homework just problem from a page............

You can use dynamic memory allocation. Learn that stuff because its good for you. There are two types of dynamic memory allocation.
1. malloc
2. new

*pls correct me if I'm wrong

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.