Help on finding an average!

Here is what I have. I have notes in the code where I'm having trouble towards the bottom. I'm trying to find the total average of the grades entered by the user. The user has put in four different grades near the bottom. They are weighted as 10% of the grade. the Programs are 30% and the exercises are 10%. The final Exam is 30%. Please just give me advice on what to do. Don't tell me the code. I want to try and figure the code out on my own before asking for additional help so that I learn this. I just want advice on what steps to take next. Thanks.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    #include <iostream>
    #include <string>
    #include <vector>
    #include <numeric>
    using namespace std;

    float average(const vector<float> &values);

    int main()
    {

    
        vector<float> exerciseGradesVector;
        float exerciseGrades;
    
        cout << "Enter the grades for the Exercises. Type 1000 when you are finished." << endl;
        while ( std::cin >> exerciseGrades && exerciseGrades != 1000 )
    	{
    		exerciseGradesVector.push_back (exerciseGrades);
    	}
        cout << endl;
    
        cout << "You entered " << exerciseGradesVector.size() << " grades." << endl;
        cout << endl;
        cout << "The Exercise grades you entered are : ";
        for(int i=0; i < exerciseGradesVector.size(); i++)
        {
	    	cout << exerciseGradesVector[i] << "  ";
	    }
	    cout << endl;
    	float averageExerciseGrades = average( exerciseGradesVector );
	    cout << "The average of the Exercise grades is: " << averageExerciseGrades << endl;
        cout << endl;	
	
	    vector<float> programGradesVector;
        float programGrades;
        cout << "Enter the grades for the Programss. Type 1000 when you are finished." << endl;
        while ( std::cin >> programGrades && programGrades != 1000 )
	    {
	    	programGradesVector.push_back (programGrades);
	    }
        cout << endl;
        cout << "You entered " << programGradesVector.size() << " grades." << endl;
       cout << endl;
        cout << "The Program grades you entered are : ";
        for(int i=0; i < programGradesVector.size(); i++)
        {
    		cout << programGradesVector[i] << "  ";
    	}
	    cout << endl;
	    float averageProgramGrades = average( programGradesVector );
	    cout << "The average of the Program grades is: " << averageProgramGrades << endl;
	    cout << endl;
	
	    float midTermTest;
	    float midTermProgram;
    	float finalExamTest;
    	float finalExamProgram;
	
	    cout << "Please enter the Midterm Exam score for the multipule choice section. " << endl;
	    cin >> midTermTest;
	    cout << "Please enter the Midterm Exam score for the Programming section. " << endl;
	    cin >> midTermProgram;
	    cout << "Please enter the Final Exam score for the multipul choice section. " << endl;
	    cin >> finalExamTest;
	    cout << "Please enter the Final Exam score for the Programming section. " << endl;
	    cin >> finalExamProgram;
	///////I need to find the average off ALL the grades.. All the 4 inputs above and the two vectors need to be in the average.
	///////I am not sure if I need to read the 3 numbers before the last one into a vector because each is worth 10 percent and then do the next step or what. Would that mess it up? Advice please?  
	    vector<float> allGradesVector;
	    float totalaverageGrade = average( allGradesVector ); //This is obviously not finished. Just ignore it..
	
       }


    float average( const vector<float> &values )
    {
        float sum = 0.0;

        for ( size_t i = 0; i < values.size(); i++ ) sum += values[i];
    
        return values.size() == 0 ? sum : sum / values.size();
    }
Last edited on
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
using namespace std;

float average(const vector<float> &values);

int main()
{
    cout << "Kaitlin Stevers" << endl;
    cout << "Program 11" << endl;
    cout << "December 8th 2016" << endl;
    cout << endl;
    cout << endl;
    
    vector<float> exerciseGradesVector;
    float exerciseGrades;
    
    cout << "Enter the grades for the Exercises. Type 1000 when you are finished." << endl;
    while ( std::cin >> exerciseGrades && exerciseGrades != 1000 )
	{
		exerciseGradesVector.push_back (exerciseGrades);
	}
    cout << endl;
    
    cout << "You entered " << exerciseGradesVector.size() << " grades." << endl;
    cout << endl;
    cout << "The Exercise grades you entered are : ";
    for(int i=0; i < exerciseGradesVector.size(); i++)
    {
		cout << exerciseGradesVector[i] << "  ";
	}
	cout << endl;
	float averageExerciseGrades = average( exerciseGradesVector );
	cout << "The average of the Exercise grades is: " << averageExerciseGrades << endl;
    cout << endl;	
	
	vector<float> programGradesVector;
    float programGrades;
    cout << "Enter the grades for the Programss. Type 1000 when you are finished." << endl;
    while ( std::cin >> programGrades && programGrades != 1000 )
	{
		programGradesVector.push_back (programGrades);
	}
    cout << endl;
    cout << "You entered " << programGradesVector.size() << " grades." << endl;
    cout << endl;
    cout << "The Program grades you entered are : ";
    for(int i=0; i < programGradesVector.size(); i++)
    {
		cout << programGradesVector[i] << "  ";
	}
	cout << endl;
	float averageProgramGrades = average( programGradesVector );
	cout << "The average of the Program grades is: " << averageProgramGrades << endl;
	cout << endl;
	
	float midTermTest;
	float midTermProgram;
	float finalExamTest;
	float finalExamProgram;
	
	cout << "Please enter the Midterm Exam score for the multipule choice section. " << endl;
	cin >> midTermTest;
	cout << "Please enter the Midterm Exam score for the Programming section. " << endl;
	cin >> midTermProgram;
	cout << "Please enter the Final Exam score for the multipul choice section. " << endl;
	cin >> finalExamTest;
	cout << "Please enter the Final Exam score for the Programming section. " << endl;
	cin >> finalExamProgram;
	cout << endl;
	cout << endl;
	float totalAverageGrade = (averageProgramGrades * .30) + (averageExerciseGrades * .10) + (midTermTest * .10) + (midTermProgram * .10) + (finalExamTest * .10) + (finalExamProgram * .30);
	cout << "The total weighted average is: " << totalAverageGrade << endl;
	cout << endl;
	if(totalAverageGrade < 60)
	{
		cout << "The letter grade is 'F'." << endl;
	}
	else
	if(totalAverageGrade > 59 && totalAverageGrade < 70)
	{
		cout << "The letter grade is 'D'." << endl;
	}
	else 
	if(totalAverageGrade > 69 && totalAverageGrade < 80)
	{
		cout << "The letter grade is 'C'." << endl;
	}
	else
	if(totalAverageGrade > 79 && totalAverageGrade < 90)
	{
		cout << "The letter grade is 'B'." << endl;
	}
	else
	if(totalAverageGrade > 89)
	{
		cout << "The letter grade is 'A'." << endl;
	}
	else
	cout << "There was an error in the process. Please retry and double check the input." << endl;

}
float average( const vector<float> &values )
{
    float sum = 0.0;

    for ( size_t i = 0; i < values.size(); i++ ) sum += values[i];

    return values.size() == 0 ? sum : sum / values.size();
}


I got it.


-- Maybe not... The weighted average is comping out wrong?? Suggestions??
Last edited on
Hi,

I am sorry but I think I didn't totally understood the weighted average stuff. But in your line 74

float totalAverageGrade = (averageProgramGrades * .30) + (averageExerciseGrades *.10) + (midTermTest * .10) + (midTermProgram * .10) + (finalExamTest * .10) + (finalExamProgram * .30);

Shouldn't the whole totalAverageGrade be divided by something to get an average?
Last edited on
I think your program is correct, it's just that the avergeProgramGrades and averageExerciseGrades have been switched around in order between the moments they were
recorded in the program and the totalAverageGrade equation. So if you are trying to double-check the answer with a spreadsheet you need to bear this in mind.
Topic archived. No new replies allowed.