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

For each of the following, write C++ statements that perform the specified task.

ID: 3627323 • Letter: F

Question

For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in two bytes and that the starting address of the array is at location 1002500 in memory. Declare an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume that the symbolic constant SIZE has been defined as 5. Declare a pointer vPtr that points to an object of type unsigned int. Use a for statement to print the elements of array values using array subscript notation. Write two separate statements that assign the starting address of array values to pointer variable vPtr. Use a for statement to print the elements of array values using pointer/offset notation. Use a for statement to print the elements of array values using pointer/offset notation with the array name as the pointer. Use a for statement to print the elements of array values by subscripting the pointer to the array. Refer to the fifth element of values using array subscript notation , pointer/offset notation with the array name as the pointer, pointer subscript notation and pointer/offset notation. What address is referenced by vPtr + 3? What value is stored at that location? Assuming that vPtr points to values[ 4 ], what address is referenced by vPtr - - 4? What value is stored at that location?

Explanation / Answer

a ) unsigned values[SIZE] ;

for(int i = 0 ; i < SIZE ; i ++)
  values[i] = (i*2) + 2;
--------------------------------------------------------

b ) unsigned * vptr;

---------------------------------------------------------

c) for(int i = 0 ; i < SIZE ; i ++)
  cout << values[i] << endl ;

------------------------------------------------------------

d)
vptr = values;

vptr = &values[0];

--------------------------------------------------------------

e) for(int i = 0 ; i < SIZE ; i ++)
  cout << *(vptr+i) << endl

--------------------------------------------------------------

f) for(int i = 0 ; i < SIZE ; i ++)
  cout << *(values+i) << endl;

------------------------------------------------------------

g) for(int i = 0 ; i < SIZE ; i ++)
  cout << vptr[i] << endl;

------------------------------------------------------------

h) cout << values[4] <<endl;
cout << *(values+4) << endl;

cout << vptr[4] <<endl;
cout << *(vptr + 4 ) <<endl;

-----------------------------------------------------------

i ) vptr = > 1002500
     (vptr+1) = > 1002502
     (vptr+2) = > 1002504
     (vptr+3) = > 1002506
   
    *(vptr+3) is 8

-------------------------------------------------------------

j ) vptr points to address of values[4] which is 1002508

upon decrementing vptr by 4 it moves by 8 bytes back and hence the address
it points to is 1002500

and values stored at this address is 2

--------------------------------------------------------

//sample program

#include <iostream >
#define SIZE 5
using namespace std ;

int main()
{

unsigned values[SIZE] ;

for(int i = 0 ; i < SIZE ; i ++)
  values[i] = (i*2) + 2;

unsigned * vptr;


for(int i = 0 ; i < SIZE ; i ++)
  cout << values[i] << endl ;


vptr = values;

vptr = &values[0];

for(int i = 0 ; i < SIZE ; i ++)
  cout << *(vptr+i) << endl;

//for(int i = 0 ; i < SIZE ; i ++)
// cout << *(values+i) << endl;

for(int i = 0 ; i < SIZE ; i ++)
  cout << vptr[i] << endl;


cout << values[4] <<endl;
cout << *(values+4) << endl;
cout << *(vptr + 4 ) <<endl;
cout << vptr[4] <<endl;


}

// hope this helps : ) please rate