Write a program that reads a group of numbers from the user and places them in an array of type int. Once the numbers are stored in the array, the program should average them and print the result.
#include <iostream>
int main() {
using namespace std;
// Prompt user to enter number of numbers
int N, i;
cout << "Enter number of numbers: ";
cin >> N;
// Allocate array for holding numbers
int *arr = new int[N];
// Read numbers
for ( i = 0; i < N; ++i) {
cout << "Enter number #" << (i + 1) << ": ";
cin >> *(arr + i);
}
// Find average value
float avg=0.0;
for ( i = 0; i < N; ++i) {
avg += *(arr + i);
}
avg /= N;
cout << "Average value of entered numbers is " << avg << endl;
// Free memory
delete [] arr;
return 0;
}
Comments
Leave a comment