Well, if your array will contain 100 elements you will list all elements in the expression? And what about 1000 elements?:)
So it is much better to use a loop to calculate the sum.
By the way the main shall have return type int
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
int main()
{
constint N = 5;
int array[N] = { 1, 2, 3, 4, 5 };
int sum = 0;
for ( int i = 0; i < N; i++ ) sum += array[i];
std::cout << "The average is " << sum / N << std::endl;
}
Now let assume that you need to caculate the average of an array with 100 elements. How to initialize the array in this case? The simplest way is to use standard C++ algorithms. You can use std::iota to initialize the array and std::accumulate to get the sum of array elements.
The code will look the following way
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<iostream>
#include <numeric>
int main()
{
constint N = 100;
int array[N];
std::iota( std::begin( array ), std::end( array ), 1 );
int average = std::accumulate( std::begin( array ), std::end( array ), 0 ) / N;
std::cout << "The average is " << average << std::endl;
}