Reputation: 19
I've only recently start programming and I've got this task to create a vector, ask the user for input values which are then appended to the vector and then the average is taken of the values entered.
Right now, what I have is this:
#include<vector>
#include<iostream>
#include<numeric>
using namespace std;
int main()
{
vector<float> v;
float input;
cout << " Enter values" <<endl;
while (cin >> input)
v.push_back(input);
float average = accumulate( v.begin(), v.end(), 0.0/v.size());
cout << "The average is " << average <<endl;
return 0;
}
I think I'm calculating the average wrong because when I test it, it just adds the numbers instead of finding the average. What is it that I need to change?
Upvotes: 1
Views: 149
Reputation: 87957
So you've just got the brackets wrong
float average = accumulate( v.begin(), v.end(), 0.0) / v.size();
not
float average = accumulate( v.begin(), v.end(), 0.0/v.size());
Your version did 0.0/v.size()
which obviously is zero. Instead you want to divide the result of accumulate
by the size.
Upvotes: 3