I have an assignment that is the following:
For a given integer array, find the sum of its elements and print out the final result, but to get the sum, you need to e
I know you were told to use for_each
, but I would actually do this - perhaps an alternative for extra credit ;-)
#include
using namespace std;
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = accumulate(array, array + (sizeof(array) / sizeof(int)), 0);
accumulate is expressly designed for summing the elements of an iterable range.