B.Tech. (I) - ICP-132 (2008) Assignment 10 Level 0 ------- 1. Let ptr_i be a pointer to an integer 'i'. Show how to change the value of 'i' using the pointer. 2. Print the address of ptr_i and i as given in the above example. 3. Print the address, value stored and the value stored at the pointed address for ptr_i using three different functions. Level 1 ------- 1. Let x and y be two integers with values 10 and 20. Write a function 'swap' that takes the addess of these two integers and swaps their values. 2. Let ptr_x and ptr_y be the pointers to x and y as discussed above. Write a function ptr_swap that exchanges the address of these two pointers using references. 3. Let ptr_x and ptr_y be the pointers to x and y as discussed above. Write a function ptr_swap that exchanges the address of these two pointers using pointers. Level 2 ------- 1. Let A1 be a 1-dimensional array of size 10. Show how to store the first ten odd numbers in the array. Print these values with and without using their indices! 2. In the above problem A1 is itself a pointer. Let B1 be a pointer that is A1+5. Can one access an element B1-2? Show if it is allowed and find the values of *(A1+2) and *(B1-1). 3. Let A2[4][4] be a two-dimensional matrix such that A[i][j]=(10*i)+j. What values will be printed? Also write a function mat_add that adds two matrices of order 4x5. (See "Pointer Arithmetic and Multiple Indirection" at http://www.technoplaza.net/programming/lesson9.php) Code: #include using namespace std; int main() { int A2[4][4]; for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { A2[i][j]=10*i+j; } } cout << **(A2+1) << endl; cout << *(*(A2+2)+3) << endl; return 0; } 4. Let A3[4][4][4] be a two-dimensional matrix such that A[i][j]=(10*i)+j. What values will be printed? (3-D arrays are pointer-of-pointers-of-pointers.) Code: #include using namespace std; int main() { int A3[4][4][4]; for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { for (int k=0; k<4; k++) { A3[i][j][k]=100*i+10*j+k; } } } cout << ***(A3+1) << endl; cout << **(*(A3+2)+3) << endl; cout << *(*(*(A3+2)+3)+1) << endl; return 0; } 5. Let 'add' and 'mult' be two functions with same type of arguements. Demonstrate usage of switch and pointers to function by executing add to do something if a bool variable is true and mult if it is false. 6. Let 'add' and 'mult' be two functions with same type of arguements. Demonstrate usage of switch and pointers to function by executing add to do something if a there are even number of 1s in the binary representation of an integer 'n' given as input and mult otherwise. (For instance, if 5 is stored as an integer and if integer has 4 bytes, then the binary representation could be 00000000 00000000 00000000 00000101. Here there are 2 ones and the rest 16 are zeros. 7. Demonstrate adding of two objects through overloading '+' using their pointers. 8. Connect an object of class P defined below to another object of the same class. Print the content of the second object (ob2) using the address stored in the first object (ob1). To do this, you may use the following code by completing the line with cout. Similarly demonstrate the same for a dynamically created object. Code: #include using namespace std; class C { public: int x; C *ptr_C; }; int main() { C *ob_ptr; C ob1; C ob2; ob2.x=10; ob1.ptr_C=&ob2; ob_ptr=new C; // create an object of type C (*ob_ptr).x=20; // set value of x as 20 ob2.ptr_C=ob_ptr; // store the address of this object cout ; cout ; return 0; } 9. Write a program to add n objects of class C as above and connect them in the similar way. Print all the values of the objects using their pointers stored in another objects.