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;
}
|