find average of input to vector c++
In C++17 and higher, you should prefer std::reduce
over std::accumulate
. Both calculate the sum, but std::reduce
has higher numerical stability and, less importantly, is potentially faster because it might be parallelized.
#include <vector>
#include <numeric>
#include <iostream>
float average(std::vector<float> const& v){
if(v.empty()){
return 0;
}
auto const count = static_cast<float>(v.size());
return std::reduce(v.begin(), v.end()) / count;
}
int main(){
std::vector<float> v{8, 4, 2, 7, 5};
auto const a = average(v);
std::cout << "average: " << a << "\n";
}
Note that static_cast
suppresses a compile warning here, that std::size_t
(the type of v.size()
) with 8 byte size can theoretically express values for which the precision of a 4 byte float
is not sufficient.
Your code has two bug. First get rid of semi-colon after while
while (cin >> input); // <-- bug
v.push_back(input); // not in loop
// fixed loop
while (cin >> input){
v.push_back(input);
}
Second your average calculation is wrong. The third argument of std::accumulate
is the initial value of the sum, which is 0
by default, so you don't need to pass it.
Your actual bug is, that you divided the 0 by the element count. What you wanted, was to divide the value sum by the element count.
float average = accumulate(v.begin(), v.end(), 0.0 / v.size());
// ^^^^^^^^^^^^^^
// fixed code
float average = accumulate(v.begin(), v.end(), 0.0) / v.size();
// without explicit init value
float average = accumulate(v.begin(), v.end()) / v.size();
// C++17 version with higher numerical stability
float average = reduce(v.begin(), v.end()) / v.size();
Also, the element of container data type should match the container type or if your really want to read integers and add to a float
vector, you should use static_cast
. The bulky syntax not only suppresses any compiler warnings, but also makes it clear that this is not an oversight, but an intentional difference in data types.
vector<float> v; // float vector
int input; // integer input value
// ...
v.push_back(input); // add int value to float container
// an intentional type conversion should catch the eye
v.push_back(static_cast<float>(input));
There are quite a few bugs in your code, have you actually debugged it? here's a working version:
#include <vector>
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
vector<float> v;
float input;
cout << " Please enter numbers you want to find the mean of:" <<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;
}