1) Declare an integer pointer. 2) Use the integer pointer to allocate a dynamic
ID: 3842113 • Letter: 1
Question
1) Declare an integer pointer.
2) Use the integer pointer to allocate a dynamic array of 30 integers.
3) Given the following:
double *dp, *dq;
double x = 78.9;
double y = 23.4;
dp = &x;
dq = &y;
i ) What is the value of *dp ?
ii) Write the C++ code that will swap the values of x and y, WITHOUT USING x and y in your code.
iii) Given the following code, write an expression that uses b to copy the third element of the array to the fourth element. DO NOT use a in your answer.
float a[5] = {0.0, 1.1, 2.2, 3.3, 4.4};
float *b = &a[1];
Explanation / Answer
1) Declare an integer pointer.
int *p
2) Use the integer pointer to allocate a dynamic array of 30 integers.
int * p = (int *)malloc(30 * sizeof(int))
OR
int * p = new int[30] ;
3)
i ) What is the value of *dp ?
the value of *dp = 78.9
ii) Write the C++ code that will swap the values of x and y, WITHOUT USING x and y in your code.
double temp = *dp;
*dp = *dq ;
*dq = temp;
iii) Given the following code, write an expression that uses b to copy the third element of the array to the fourth element. DO NOT use a in your answer.
float a[5] = {0.0, 1.1, 2.2, 3.3, 4.4};
float *b = &a[1];
*(b+2) = *(b+1)
Thanks, let me know if there is any doubts
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.