Write a program that asks the user to input an integer named numDoubles. Create a dynamic array that can store numDoubles doubles and make a loop that allows the user to enter a double into each array entry. Loop through the array, calculate the average, and output it. Delete the memory allocated to your dynamic array before exiting.

Respuesta :

Answer:

Answered in C++

#include <iostream>

using namespace std;

int main() {

   int size;

   cout<<"Length of array: ";

   cin >> size;

   double *numDoubles = new double[size];

   double sum =0;

   cout<<"Array Elements: ";

   for(int i=0;i<size;i++){

       cin>>numDoubles[i];

       sum += numDoubles[i];

   }

   delete [] numDoubles;

   cout<<"Average: "<<sum/size;

   return 0;  

}

Explanation:

This line declares the size (or length) of the array

   int size;

This line prompts the user for the array length

   cout<<"Length of array: ";

This gets the input from the user

   cin >> size;

This dynamically declares the array as of double datatype

   double *numDoubles = new double[size];

This declares and initializes sum to 0

   double sum =0;

This prompts the user for elements of the array

   cout<<"Array Elements: ";

The following iteration gets input from the user and also calculates the sum of the array elements

   for(int i=0;i<size;i++){

       cin>>numDoubles[i];

       sum += numDoubles[i];

   }

This deletes the array

   delete [] myarray;

This calculates and prints the average of the array

   cout<<"Average: "<<sum/size;