Question 2) For each of the following, write C++ statements that perform the spe
ID: 3750903 • Letter: Q
Question
Question 2) For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in four bytes and that the starting address of the built-in array is at location 1002500 in memory.
a. Declare an unsigned int built-in array values with five elements initialized to the even integers from 2 to 10. Assume that the constant size has been defined as 5.
b. Declare a pointer vPtr that points to an object of type unsigned int.
c. Use a for statement to display the elements of built-in array values using array subscript notation.
d. Write two separate statements that assign the starting address of built-in array values to pointer variable vPtr.
e. Use a for statement to display the elements of built-in array values using pointer/offset notation.
f. Use a for statement to display the elements of built-in array values using pointer/offset notation with the built-in array’s name as the pointer.
g. Use a for statement to display the elements of built-in array values by subscripting the pointer to the built-in array.
h. Refer to the fifth element of values using array subscript notation, pointer/offset notation with the built-in array’s name as the pointer, pointer subscript notation and pointer/offset notation.
i. What address is referenced by vPtr + 3? What value is stored at that location?
j. Assuming that vPtr points to values[4], what address is referenced by vPtr -= 4? What value is stored at that location?
Explanation / Answer
2)
a)
int values[SIZE] = {2,4,6,8,10};
b)
int *vPtr;
c)
for(i=0;i<SIZE;i++)
printf("%d", values[i]);
d)
vPtr=values;
vPtr=&values[0];
e)
for(i=0;i<SIZE;i++)
printf("%d", *(vPtr+i));
f)
for(i=0;i<SIZE;i++)
printf("%d", *(values+i));
g)
for(i=0;i<SIZE;i++)
printf("%d ", vPtr[i]);
h)
array subscript notation: values[4]
pointer/offset notation: *(values +4)
pointer subscript notation: vPtr[4]
pointer/offset notation: *(vPtr + 4)
i)
1002512 is the address is referenced by vPtr + 3 and 8 is the value stored at that location.
j)
1002500 is the address referenced by vPtr -= 4 and 2 is stored at that location.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.