Exercise 1) write c++ statements to allow the user to enter n, the number of val
ID: 3569355 • Letter: E
Question
Exercise 1) write c++ statements to 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 doublPtr
Answer:
2) fill the annonymous array of exercise 1 with n input values, entered from the keyboard.
3) Compute and display the average of the values in the annonymous array of exercise 1
4)deallocate the storage of the anonymous array of exercise 1.
5) In ecercise 1, if the value of doubleptr is 1000 and double values are stored in 8 bytes, draw a memory map showing the address of the first few elements of the annonymous array.
6) Assuming the address are stored in 4 bytes and double value in 8 bytes, tell what output will be produced by the following statemnt:
double dubValues[] = { 1.1, 2.2, 3.3, 4.4, 5.5};
double * dubPtr = dubValues;
for (int=i; i<5; i++)
cout<<sizeof(dubPtr + i) <<" "
<< sizeof(*(dubPtr +i))<< " "
<< *(dubPtr + i) << endl;
Explanation / Answer
#include<iostream>
using namespace std;
void main(void)
{
double *doublPtr;
int n;
cout << "enter no of elements to be processed: " ;
cin>>n;
doublPtr =new double[n];
cout<<" ";
double total=0;
cout<<"Enter numbers: ";
for(int i=0; i<n; i++)
{
cout<<"Enter "<<(i+1)<<" number: ";
cin>>doublPtr[i];
total+=doublPtr[i];
}
cout<<" Average is: "<<(double)(total/n)<<" ";
//arrays are freed-up like this
delete []doublPtr;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.