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.
1
Expert's answer
2015-01-30T03:39:56-0500
// 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; }
Comments
Leave a comment