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:

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
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:

1
2
3
4
5
#include <numeric>

...

    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!

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';
}



3.5

Last edited on
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <valarray>
using namespace std;

int main()
{
   valarray<int> 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:

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
@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.
Topic archived. No new replies allowed.