Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

7.9 Answer each of the following. Assume that unsigned integers are stored in 2

ID: 3711113 • Letter: 7

Question

7.9 Answer each of the following. Assume that unsigned integers are stored in 2 bytes and that the starting address of the array is at location 1002500 in memory. a. Define an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume the symbolic constant SIZE has been defined as 5. b. Define a pointer vPtr that points to an object of type unsigned int. c. Print the elements of array values using array index notation. Use a for statement and assume integer control variable i has been defined. d. Give two separate statements that assign the starting address of array values to pointer variable vPtr. e. Print the elements of array values using pointer/offset notation f. Print the elements of array values using pointer/offset notation with the array name as the pointer. g. Print the elements of array values by indexing the pointer to the array. h. Refer to element 5 of array values using array index notation, pointer/offset notation with the array name as the pointer, pointer index notation, and pointer/offset notation i. What address is referenced by vPtr 3? What value is stored at that location? j. Assuming vPtr points to values [ 4], what address is referenced by vPtr- 4? What value is stored at that location?

Explanation / Answer

Answer a:
int values[5] = {2,4,6,8,10};
Explanation:
Array values is initialized with 5 even numbers

Answer b:
int *vPtr;
Explanation:
vPtr is a pointer to point to an integer object, we must declare it as type int*. The first part int is where we are determining the data type the pointer is pointing to, and the second part * is how we make the variable a pointer.

Answer c:
for (i = 0; i < SIZE; i++)
printf ("%d ", values[i]);

Explanation:
Using for loop to print all the the elements the elements of the array

Answer d:
vPtr = values;
vPtr = &values[0];

Explanation:
Since values is the address of the first element of the array, this is a valid way to assign the first element in the values array to the vPtr pointer. Take care not to use the * operator when you really want an address, as in this case.
Since values[0] is the first element of the array, we use &values[0] as the starting address of the array to assign to vPtr


Answer e:
for (i = 0; i < SIZE; i++)
printf ("%d ", *(vPtr + i));

Explanation:
Using for loop along with the pointer to print all the elements in an array.


Answer f:

for (i = 0; i < SIZE; i++)
printf ("%d ", *(values + i));

Explanation:
Using for loop along with the pointer to print all the elements in an array.


Answer g:

for (i = 0; i < SIZE; i++)
printf ("%d ", vPtr[i]);

Explanation: Printing the elements of array by indexing the pointer

Answer i: 1002512 and 8
Explanation:
Since values[0] is stored at 1002500 and vPtr + 3 refers to values[3], and an integer is 4 bytes long, the address that is referenced by vPtr + 3 is 1002500 + 3 * 4 = 1002512. Since we are referring to values[3] we are referring to the fourth element in the values array, thus the value we are referring to is 8.

Answer j : 1002500 and 2
Explanation:
Since vPtr point to values[4], the address stored in vPtr is 1002500 + 4 * 4 = 1002516 (since each element is an integer using 4 bytes). vPtr -= 4 decrements vPtr by 4 elements of the array, with a new value of 1002516 - 4 * 4 = 1002500. This is the address of the first element of the array, which is 2.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote