Question #50597

Write a program that reads a group of numbers from the user and places them in an array of type float. Once the numbers are stored in the array, the program should average them and print the result. Use pointer notation wherever possible.

Expert's answer

// main.cpp -- reads group of numbers and finds average of them
#include <iostream>
int main()
{
using namespace std;
// Prompt user to enter number of numbers
int length;
cout << "Enter number of numbers: ";
cin >> length;
// Allocate array for holding numbers
float *arr = new float[length];
// Read numbers
for (int i = 0; i < length; ++i)
{
cout << "Enter number #" << (i + 1) << ": ";
cin >> *(arr + i);
}
// Find average value
float average = 0.0F;
for (int i = 0; i < length; ++i)
{
average += *(arr + i);
}
average /= length;
cout << "Average value of entered numbers is " << average << endl;
// Free memory
delete [] arr;
return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS