ASSIGNMENT 1 p1 A. Programming Output For each of the given program segments, read the code and write the output in the space provided below each program. 1. What is output by the following program segment? 1 int number = 9; 2 int *p = &number; // address of number is 0012FF8C 3 4cout << number << " " << *p << " " << p; answer 2. What is output by the following program segment? 1 int a[] = { 2, 4, 6, 8, 10 }; 2 int *p = a; 3 4 cout << a[ 0 ] << " " << *( p + 1 ) << " " << p[ 2 ]; answer: 3. What is output by the following program segment? 1 int a[] = { 1, 2, 3, 4, 5 }; 2 int *p = a; 3 4 cout << a[ 0 ]; 5 cout << *(a+1); 6 cout << *(p + 2); 7 cout << p[ 3 ]; answer: 4. What is output by the following program segment? 1 int i,a[] = { 1, 2};// address of a[0] is 1000 2 int *p = a; 3 4 for(i=0;i<=1;i++) 5 6 cout << (p+i)<<” “<<*(p+i)<<endl; answer: Faculty of Computer, Media & Technology TATi UNIVERSITY COLLEGE p2 B. Correct the Code For each of the given program segments, determine if there is an error in the code. If there is an error, specify whether it is a logic or compilation error, circle the error in the program, and write the corrected code in the space provided after each problem. If the code does not contain an error, write “no error.” [Note: It is possible that a program segment may contain multiple errors.] 1. The following declarations should declare three pointers: 1 int *ptr1; 2 int &ptr2; 3 int ptr2; answer: Errors: 2. The following code should display m and the contents of m via the pointer: 1 int m = 9; 2 int *mptr = &m; 3 4 cout << *m << mptr answer: Errors: 3. The following code should print the elements of the array, using pointer arithmetic: 1 int a[ 5 ] = { 1, 2, 3, 4, 5 }; 2 int *aptr = &a[ 0 ]; 3 4 for ( int i = 0; i <= 5; i++ ) 5 cout << *( aptr + i ); answer: Error: Faculty of Computer, Media & Technology TATi UNIVERSITY COLLEGE p3 C. Coding Exercises For each of the following problems, write a program or a program segment that performs the specified action. 1. Write three lines of code that declare three pointers to integers. Initialize one to NULL, another to 0 and the third to point to the address of int variable x. answer: 2. Using the pointer you declared in Coding Exercise 1, that points to x, modify x by assigning 100 to it. answer: 3. Write a function prototype for the function fns that takes three pointers to integers as arguments and returns nothing. answer: 4. Apply the sizeof operator to all pointers from Coding Exercise 1. answer: 5. Traverse the content of the following array using pointer-offset notation, and display the all value of j_array seperated by space. 1 int *j j_array={1,2,3,4,5}; 2 j=j_array; // j point to j_array answer: 6. Traverse the contents of the string from Coding Exercise 5, this time using pointer subscript notation. In this exercise, each value output should be separated from adjacent characters by a '-' character. answer: Faculty of Computer, Media & Technology TATi UNIVERSITY COLLEGE p4 7. 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. a. 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. b. Declare a pointer vPtr that points to an object of type unsigned int. c. Use a for statement to print the elements of array values using array subscript notation. d. Write two separate statements that assign the starting address of array values to pointer variable vPtr. e. Use a for statement to print the elements of array values using pointer/offset notation. f. Use a for statement to print the elements of array values using pointer/offset notation with the array name as the pointer. g. Use a for statement to print the elements of array values by subscripting the pointer to the array. h. 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. 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? Your answer: DUE DATE : 02 FEBRUARI 2008 Students are required to send the electronic copy via e-mail (nazri.ibrahim@gmail.com) before the due date and hand in the hard copy as well in person. The first day an assignment is late, the grade will drop by ten points. The second day an assignment is late, the grade will drop by an additional ten points. No assignments will be accepted more than two days late. You may download softcopy of assignment1 at http://tatiuc.edu.my/nazri/dct1063 Faculty of Computer, Media & Technology TATi UNIVERSITY COLLEGE