#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
float avg;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " integers: ";
for(int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
avg = (float)sum / n;
cout << "The average is " << avg << endl;
return 0;
}
Enter the number of elements: 5
Enter 5 integers: 21
34
15
39
54
19
The average is 28.6
integer array
of that size
, and prompts the user to enter integers
for the array
. sum
of the integers in the array and divides by the number of elements
to find the average
. Finally, it prints out the average
.