C++ 1) Pointer basics a) Declare two (type double) pointer variables named d_var
ID: 3818955 • Letter: C
Question
C++
1) Pointer basics
a) Declare two (type double) pointer variables named d_var and d_array:
b) Write C++ statements to dynamically create a double-precision floating-point variable and store its address in d_var. Also dynamically create an array of 10 double-precision floating-point values and store its address in d_array:
c) Write C++ statements to input a value for d_var (i.e., a value that d_var points to) from the console and then display it:
d) Write C++ statements to initialize the 10 double values in the dynamically allocated array to 1.0 :
e) Now write C++ statements to de-allocate the memory (i.e. using the delete operator) pointed by d_var and d_array
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
// a
double *d_var, *d_array;
// b
double c = double(1);
d_var = &c;
d_array = new double[10];
// c
cin >> *d_var;
cout << *d_var<< endl;
// d
for(int i = 0; i < 10; i++)
{
*(d_array + i) = 1.0;
}
for(int i = 0; i < 10; i++)
{
cout << *(d_array + i) << " ";
}
cout << endl;
//e
delete [] d_array;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.