Write C++ statements to do the follwing: 1. Allow the user to enter n , the numb
ID: 3779766 • Letter: W
Question
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,i;
cout<<"Enter number :";//reading n from keyboard
cin>>n;
double *doublePtr = new double(sizeof(double)*n);//creating anonymous array
cout<<"Enter "<<n<<"values :";
for(i=0;i<n;i++)cin>>doublePtr[i];//reading n values from keyboard
cout<<"Displaying addresses of elements in anonymous array: ";
//cout<<doublePtr+1;
for(i=0;i<n;i++)cout<<doublePtr+i<<" ";//printing address
double av=0;
for(i=0;i<n;i++)av=av+doublePtr[i];//computing average
av = av/n;
//displaying average
cout<<" The average is:"<<av<<" ";
free(doublePtr);
char y;
cout<<"Enter y:if you want to do again, else n:";
cin>>y;
if(y=='y')main();
return 0;
}
ouput:-
Enter number :5
Enter 5values :1
2
3
4
5
Displaying addresses of elements in anonymous array:
0xae5f80 0xae5f88 0xae5f90 0xae5f98 0xae5fa0
The average is:3
Enter y:if you want to do again, else n:y
Enter number :3
Enter 3values :4
3
2
Displaying addresses of elements in anonymous array:
0xae5f80 0xae5f88 0xae5f90
The average is:3
Enter y:if you want to do again, else n:y
Enter number :2
Enter 2values :1
2
Displaying addresses of elements in anonymous array:
0xaee0f0 0xaee0f8
The average is:1.5
Enter y:if you want to do again, else n:n
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.