using accumulate and vectors is pretty neat also:
Code:int num; vector<int> nums; while (cin >> num ) nums.push_back(num); double avg = accumulate( nums.begin(), nums.end(), 0.0 ) / nums.size();
This is a discussion on Averaging code within the C++ Programming forums, part of the General Programming Boards category; using accumulate and vectors is pretty neat also: Code: int num; vector<int> nums; while (cin >> num ) nums.push_back(num); double ...
using accumulate and vectors is pretty neat also:
Code:int num; vector<int> nums; while (cin >> num ) nums.push_back(num); double avg = accumulate( nums.begin(), nums.end(), 0.0 ) / nums.size();
It is pretty much what Neo1 suggested, and just as wasteful if the only thing needed is to compute the arithmetic mean.using accumulate and vectors is pretty neat also:
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If i needed to find the mean, I wouldn't have used that code...
just something interesting to learn about.