May 12, 2022 at 5:36pm UTC
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <vector>
using namespace std;
double CalcAverage(vector<int > nums) {
if (nums.empty()) {
return 0;
}
return (nums.begin(), nums.end(),0) / nums.size();
}
int main() {
vector<int > 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 May 12, 2022 at 6:00pm UTC
May 12, 2022 at 6:10pm UTC
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.
May 12, 2022 at 6:39pm UTC
It compiles just fine! Returns static_cast<double>(0 / nums.size()), i.e. 0.
May 12, 2022 at 7:24pm UTC
The average of an empty set of numbers is not zero.
May 13, 2022 at 8:45am UTC
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!
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <vector>
#include <numeric>
double CalcAverage(const std::vector<int >& 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' ;
}
Last edited on May 13, 2022 at 8:45am UTC
May 13, 2022 at 9:16am UTC
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:
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
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<int > nums1 { 1, 2, 3, 4, 5, 6 };
for (const int n : nums1)
{
mean.update(n);
}
std::cout << mean.result() << std::endl;
const std::vector<int > 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 May 13, 2022 at 9:18am UTC
May 13, 2022 at 9:27am UTC
@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 May 13, 2022 at 9:43am UTC
May 13, 2022 at 10:50am UTC
I think it depends how many numbers you have and how big those can get.