In: Computer Science
1. 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.
2. Combine the statements in part 1 into one C++ program. Here is a sample output of your program.
Part C: Printing the using array subscript notation: Elem. Value Addresses 0 2 00AFFC4C 1 4 00AFFC50 2 6 00AFFC54 3 8 00AFFC58 4 10 00AFFC5C Part E: Printing the array using pointer/offset notation: Elem. Value Addresses 0 2 00AFFC4C 1 4 00AFFC50 2 6 00AFFC54 3 8 00AFFC58 4 10 00AFFC5C Part F: Printing the array using pointer/offset notation with the built-in arrays' name as the pointer Elem. Value Addresses 0 2 00AFFC4C 1 4 00AFFC50 2 6 00AFFC54 3 8 00AFFC58 4 10 00AFFC5C Part G: Printing the array by subscripting the pointer to the built-in array Elem. Value Addresses 0 2 00AFFC4C 1 4 00AFFC50 2 6 00AFFC54 3 8 00AFFC58 4 10 00AFFC5C Part H. Printing the fifth element of values using: 1. Array subscript notation: 10 2. Pointer/offest notation with array name as a pointer: 10 3. Pointer subscript notation: 10 4. Pointer/offset notation: 10 Part I. The address of vPtr + 3 : 00AFFC58 The values storedat vPtr + 3 : 8 Part J. The address of vPtr -= 4 : 00AFFC4C The values stored at vPtr -= 4 : 2 Press any key to continue . . . |
#include <iostream>
using namespace std;
int main()
{
// (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.
unsigned int A[5]={2,4,6,8,10};
// (B) Declare a pointer vPtr that points to an object of type unsigned int.
unsigned int *vPtr;
//(C) Use a for statement to display the elements of built-in array values using array subscript notation.
cout<<"Elem.\t Value\t Addresses"<<endl;
for(int i=0;i<5;i++)
cout<<i<<"\t "<<A[i]<<"\t"<<&A[i]<<endl;
//(D) Write two separate statements that assign the starting address
//of built-in array values to pointer variable vPtr.
vPtr=&A[0];
vPtr=A;
//(E)Use a for statement to display the elements of built-in array values using pointer/offset notation.
cout<<"Elem.\t Value\t Addresses"<<endl;
for(int i=0;i<5;i++)
cout<<i<<"\t "<<*(vPtr+i)<<"\t"<<(vPtr+i)<<endl;
//(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.
cout<<"Elem.\t Value\t Addresses"<<endl;
for(int i=0;i<5;i++)
cout<<i<<"\t "<<*(A+i)<<"\t"<<(A+i)<<endl;
//(G)Use a for statement to display the elements of built-in array values
//by subscripting the pointer to the built-in array.
cout<<"Elem.\t Value\t Addresses"<<endl;
for(int i=0;i<5;i++)
cout<<i<<"\t "<<vPtr[i]<<"\t"<<&(vPtr[i])<<endl;
//(H) Refer to the fifth element of values using array subscript notation,
//pointer/offset notation with the built-in array name’s as the pointer,
//pointer subscript notation and pointer/offset notation.
cout<<"Printing the fifth element of values using:"<<endl;
cout<<"1. Array subscript notation:"<<A[4]<<endl;
cout<<"2. Pointer/offest notation with array name as a pointer:"<<*(A + 4)<<endl;
cout<<"3. Pointer subscript notation:"<< vPtr[4]<<endl;
cout<<"4. Pointer/offset notation: "<<*(vPtr + 4)<<endl;
//(I) What address is referenced by vPtr + 3? What value is stored at that location?
// (vptr+3) points to memory of 4th element which is 1002512 (if in decimal)
//100250C (if in hexadecimal). value stored at that location is 8.
cout<<"The address of vPtr + 3 :"<<(vPtr + 3) <<endl;
cout<<"The values storedat vPtr + 3 :"<<*(vPtr + 3) <<endl;
//(J) Assuming that vPtr points to values[4], what address is referenced by
//vPtr -= 4? What value is stored at that location?
//vPtr references Address: 1002500; value stored is 2 ;
vPtr=&A[4]; //vPtr points to values[4],
unsigned int *cpy = vPtr; //cpy points to 0th element
cout<<"The address of vPtr -= 4 :"<<(vPtr -= 4)<<endl;
// vPtr=vPtr-4(move backward 4 elent positions), it points to 0th element.
vPtr=cpy; // cpy copied to vPtr
cout<<"The values stored at vPtr -= 4 :"<<*(vPtr -= 4)<<endl;
//prints the value of 0th element.
cout<<"Press any key to continue . . .";
}
At run time the storage addresses may vary.
Array |
A[0] |
A[1] |
A[2] |
A[3] |
A[4] |
Address(in decimal) |
1002500 |
1002504 |
1002508 |
1002512 |
1002516 |
Address(hexa) |
1002500 |
1002504 |
1002508 |
100250C |
1002510 |
Values |
2 |
4 |
6 |
8 |
10 |
Since each int is 4 bytes size, next element address=current address +4
vPtr ++ or vPtr=vPtr+1, vPtr advances to address of next element .i.e of vptr= 1002500, then vPtr++ will point to 1002504(address of next element) , not point to 1002501