How do you use for loops inside vectors?

For my project the user continuously enters grades into the program. Each time a grade is entered the program should print all grades that have been entered so far, the current average of the grades thus far, and the current median of the grades thus far.

I have no idea how to do the median part but the first two i think i have something for. Im not very familiar with vectors and their tricks but i understand a little. Heres my code so far.. i know its probably way off, so any help to restructure or any vector tips is greatly appreciated.

btw the program should print something that looks like this..

Please enter a grade.
'enter 87'
your current grades are: 87
your current average is: 87
the current median is: 87

please enter a grade.
'enter 94'
your current grades are: 87 94
your current average is: 90.5
the current median is: 90.5

please enter a grade.
'enter 84'
your current grades are: 87 94 84
your current average is: 88.3
the current median is: 87

... and so on

im doing a total of ten grades.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector <int> grades;
    
    double grade, totalScore, average, median;
    
    totalScore = 0;
    
    for (int i = 1; i <= 10; i++)
    {
        cout << "Enter a grade: " << endl;
        cin >> grade;
        grades.push_back(grade);
        
        cout << "Your current grades are: ";
        
        for (int i = 0; i < grades.size(); i++)  // grades.size() is the size of the vector
        {
            cout << grades[i] << " ";
            totalScore += grades[i];
        }
        
        cout << endl;
        
        cout << "Your current average is: ";
        
        for (int i = 0; i < grades.size(); i++)
        {
            average = totalScore / grades.size();
            cout << average << " ";
        }
        
        cout << endl;
    }
    
 return 0;

}


1
2
3
vector <int> grades;
    
    double grade


You have a vector of type int and you are storing double type, this will lead to mathematical precision problems.

for (int i = 1; i <= 10; i++) get in the habbit of starting at 0 when doing this type of loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
for (int i = 1; i <= 10; i++)
    {
        cout << "Enter a grade: " << endl;
        cin >> grade;
        grades.push_back(grade);
        
        cout << "Your current grades are: ";
        
        for (int i = 0; i < grades.size(); i++)  // grades.size() is the size of the vector
        {
            cout << grades[i] << " ";
            totalScore += grades[i];
        }
        
        cout << endl;
        
        cout << "Your current average is: ";
        
        for (int i = 0; i < grades.size(); i++)
        {
            average = totalScore / grades.size();
            cout << average << " ";
        }
        
        cout << endl;
    }


You have declared 3 i variables in this code, this will cause errors. Either change the outer loop variable or change both the inner loop variables. The scope of the outer i variable continues into each of the inner loops.
Last edited on
You have declared 3 i variables in this code, this will cause errors.

Actually in this case, he is lucky and it works because the last inner loop will match what it should be for the outer loop. That won't happen often though and yes they should be different.
I would restructure it to just use one inner loop though instead of two, or three once mean is added.
Topic archived. No new replies allowed.