1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

const int N = 15 ;
const int array[N] = { 1, 23, 4, 56, 7, 8, 9, 87, 65, 43, 21, 23, 4, 56, 7 } ;
int sum = 0 ;
// recommended  rangebased loop: for each value in array, add to sum
for( int value : array ) sum += value ;
std::cout << "sum: " << sum << " average: " << double(sum) / N << '\n' ;
sum = 0 ;
// classical forloop: for value at each position in the in array, add to sum
for( int i = 0 ; i < N ; ++i ) sum += array[i] ;
std::cout << "sum: " << sum << " average: " << double(sum) / N << '\n' ;
sum = 0 ;
// classical forloop using iterator (pointer)
for( auto iter = std::begin(array) ; iter != std::end(array) ; ++iter ) sum += *iter ;
std::cout << "sum: " << sum << " average: " << double(sum) / N << '\n' ;
 