Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Dynamic Arrays Write C++ statements to do the follwing: 1. Allow the user to ent

ID: 3694086 • Letter: D

Question

Dynamic Arrays

Write C++ statements to do the follwing:

1. Allow the user to enter n, the number of values to be processed; then allocate an anonymous array of n double values, storing its address in doublePtr.

2. Fill the anonymous array of 1 with n input values, entered form the keyboard

3. Display the address of each element in the anonymous array

4. Compute and display the average of the values in the anonymous array

5. Deallocate the storage of the anonymous array

6. Prompt the user if he/she likes to do another round of work in 1 through 5

Explanation / Answer

#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{
int n,flag=1;
do
{
cout<<"Enter the number of values to be processed:";
cin>>n;
double *doublePtr = (double *)malloc(sizeof(double)*n);
for(int i=0;i<n;i++)
{
cout<<"Enter the input values number "<<i+1<<":";
cin>>doublePtr[i];
}
for(int i=0;i<n;i++)
{
cout<<&doublePtr[i]<<endl;
}
double sum=0;
for(int i=0;i<n;i++)sum+=doublePtr[i];
cout<<"The average of all the numbers is "<<sum/n<<endl;
free(doublePtr);
cout<<"Do you want to repeat?(1 to repeat and 0 to stop):";
cin>>flag;
}while(flag==1);
return 0;
}