Reputation: 145
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.
#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;
///////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();
}
Upvotes: 1
Views: 234
Reputation: 29
I would suggest to use while
to replace do while
. Because you may input 1000 firstly.
You can write like this:
while ( std::cin >> exerciseGrades && fabs(exerciseGrades - 1000) < 0.000001 )
{
exerciseGradesVector.push_back (exerciseGrades);
}
I use fabs
function to solve float
is inaccuracy in sometime.
About averages function:
You can use std::accumulate:
float average = accumulate(programGradesVector.begin(), programGradesVector.end(), 0.0) / programGradesVector.size();
or implement it by yourself like this:
float average( const std::vector<float> &values )
{
float sum = 0.0f;
int nSize = values.size();
for(int i = 0; i < nSize; ++i)
{
sum += values[i];
}
return nSize == 0 ? sum : sum / nSize;
}
Upvotes: 1
Reputation: 311038
I would suggest to rewrite the loops like this
do
{
cin >> exerciseGrades;
exerciseGradesVector.push_back (exerciseGrades);
}
while (exerciseGrades != 1000);
the following way
while ( std::cin >> exerciseGrades && exerciseGrades != 1000 )
{
exerciseGradesVector.push_back (exerciseGrades);
}
In this case there is no need to use a strange variable like dropOne
.
To find the average is easy. The function can look the following way
float average( const std::vector<float> &values )
{
float sum = 0.0f;
for ( float x : values ) sum += x;
return values.size() == 0 ? sum : sum / values.size();
}
Or if the compiler does not support the range-based for loop you can write
float average( const std::vector<float> &values )
{
float sum = 0.0f;
for ( size_t i = 0; i < values.size(); i++ ) sum += values[i];
return values.size() == 0 ? sum : sum / values.size();
}
To call the function you can write for example in main
float averageExerciseGrades = average( exerciseGradesVector );
The function must be declared before its usage that is for example before main
float average( const std::vector<float> &values );
though it may be defined after main.
Upvotes: 2
Reputation: 491
You can use std::accumulate
float average = accumulate(programGradesVector.begin(), programGradesVector.end(), 0.0) / programGradesVector.size();
Upvotes: 1
Reputation: 605
A simple for loop will do the trick.
float total = 0;
int count = 0
for(int i = 0; i < dropOnee; i++)
{
total+= programGradesVector[i];
count++;
}
cout << "The average is: " total/count << endl;
I'd also recommend using more descriptive name's for the counts.
Upvotes: 1