### Calculate Average Complete the CalcAverage() function that has an integer vector parameter and returns the average value of the elements in the vector as a double.

Ex: If the input vector is:

1 2 3 4 5
then the returned average will be:

3.0

My Code:

 ``123456789101112131415161718`` ``````#include #include using namespace std; double CalcAverage(vector nums) { if (nums.empty()) { return 0; } return (nums.begin(), nums.end(),0) / nums.size(); } int main() { vector nums = {1, 2, 3, 4, 5}; cout << CalcAverage(nums) << endl; return 0; } ``````

The compiler outputs as 0 and I don’t why. is there are reason to this?
Last edited on I can't work out how your code compiled! Unexpected use of the comma operator.

Anyway, I think you need std::accumulate around your bracket to do your sum (for which you will need #include <numeric>.

You should also cast e.g. the denominator to a (double), or you will suffer from integer division. It compiles just fine! Returns static_cast<double>(0 / nums.size()), i.e. 0. The average of an empty set of numbers is not zero. What you probably mean on line 10 is:

 ``12345`` ``````#include ... return std::accumulate(nums.begin(), nums.end(),0) / nums.size();`````` Also nums is passed by value and not by const ref. Hence there is an unneeded copy of the vector done.

Also, as the initial value for accumulate is 0, then the return value is an int!

 ``12345678910111213`` ``````#include #include #include double CalcAverage(const std::vector& nums) { return nums.empty() ? 0.0 : std::accumulate(nums.begin(), nums.end(), 0.0) / nums.size(); } int main() { const std::vector nums {1, 2, 3, 4, 5, 6}; std::cout << CalcAverage(nums) << '\n'; }``````

 ``` 3.5 ```

Last edited on ``123456789`` ``````#include #include using namespace std; int main() { valarray nums = { 1, 2, 3, 4, 5, 6 }; cout << ( nums.sum() + 0.0 ) / nums.size() << '\n'; } `````` Also one could use an "online" algorithm (Welford) to compute the mean/average, so that we do not have to sum up all numbers at once; can easily be extended to also compute variance in one pass:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````class Mean { public: Mean() : count(0U), mean(0.0) { } void update(const double newValue) { const double delta = newValue - mean; mean += delta / ++count; } double result() { return mean; } private: size_t count; double mean; }; int main() { Mean mean; const std::vector nums1 { 1, 2, 3, 4, 5, 6 }; for (const int n : nums1) { mean.update(n); } std::cout << mean.result() << std::endl; const std::vector nums2{ 7, 8, 9, 10, 11, 12 }; for (const int n : nums2) { mean.update(n); } std::cout << mean.result() << std::endl; return 0; }``````
Last edited on @kigar64551,
Are you sure that this is a good idea when the numbers that you are sending are ints? There is no loss of precision when summing up integers (although, I guess you might exceed a maximum value if you tried really hard) and you end up doing an awful lot of divide operations.
Last edited on I think it depends how many numbers you have and how big those can get.