Answer to Question #333916 in C++ for Anu

Question #333916

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.


1
Expert's answer
2022-04-26T09:27:15-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS