Princess Nora University Faculty of Computer and Information Science CS 2 4 0 Part1: Paper-and-Pencil Exercises: U Trace and then choose the right answer a, b, or c: U 1. int a; int* p; a = 2; p = &a; a = a + 1; cout << *p; a) 2 b) 3 c) Won't run 2. int a; int* p; a = 2; p = a; a = a + 2; cout << *p; a) 2 b) 4 c) Won't run 3. int a; int b; int* p; p = &a; *p = 4; p = &b; *p = 3; cout << a << “ “ << b; a) 4 3 b) 3 3 c) Won't run 4. int a; int b; int* p; int* q; a = 3; p = &a; q = p; *q = *q + 5; cout << *p; a) 8 b) 3 c) Won't run 5. int a; int* p; a = 4; p = &a; Page 1 of 5 Pointers Self Study Exercises Model Answer Princess Nora University Faculty of Computer and Information Science CS 2 4 0 cout << (*p) / a; a) 1 b) 4 c) Won't run 6. string s; string* p; s = “Fred Jones”; p = &s; cout << *p; a) Fred Jones b) Fred c) A hexadecimal memory address 7. string s; int* i; s = “Fred Jones”; i = &s; cout << *i; a) Fred Jones b) A garbage number c) Won't run 8. void doubleref(int* p) {(*p) = (*p) * 2;} int main() { int a = 5; doubleref(&a); cout << a; } a) 5 b) 10 c) Won't work 9. int a; int b; int* p; int* q; a = 3; p = &a; q = p; b = 4; *q = b; cout << *p << a; a) 4 3 b) 3 4 c) 4 4 10. int a; int* p; a = 3; p = &a; cout << p; a) 3 3 b) A hexadecimal memory address c) Won’t run Page 2 of 5 Pointers Self Study Exercises Model Answer Princess Nora University Faculty of Computer and Information Science CS 2 4 0 Pointers Self Study Exercises Model Answer 11. int a; int* p; a = 4; p = &a; cout << (*p+1); a) 4 b) 5 c) Random Garbage 12. int a; int* q; a = 4; q = &a; cout << *(q+1); a) 4 b) 5 c) Random Garbage 13. Consider the following statements: int *p; int i; int k; i = 42; k = i; p = &i; After these statements, which of the following statements will change the value of i to 75? a) k = 75; b) *k = 75; c) p = 75; d) *p = 75; e) Two or more of the answers will change i to 75 Page 3 of 5 Princess Nora University Faculty of Computer and Information Science CS 2 4 0 Pointers Self Study Exercises Model Answer Part2: coding exercises: U For each of the following Exercises, use the pointer notation (*) ONLY. Do NOT use the array index [] notation. 1. Write a function countEven(const int* a, int size) which receives an integer array and its size, and returns the number of even numbers in the array. Your main function should be as follows: void main() { int a[]={10,13,15,20,22,17}; cout<<countEven(a, 6)<<endl; } #include<iostream> using namespace std; int countEven(const int * a, int n) { int counter = 0; for(int i=0; i<n;i++) if (*(a+i)%2==0) counter++; return counter; } void main() { int a[]={10,13,14,20,22,27}; cout<<countEven(a, 6)<<endl; } 2. Write a function myStrLen(const char*) which returns the length of the parameter cstring. Write the function without using the C++ function strlen. Your main function should be as follows: void main() { const char * myString = "Hello, I love pointers"; cout << myStrLen(myString)<<endl; } #include<iostream> using namespace std; int myStrLen(const char* a) { int i; for (i=0; *a++!=NULL ; i++); return i; } void main() { const char * myString = "Hello, I love pointers"; cout << myStrLen(myString)<<endl; } Page 4 of 5 Princess Nora University Faculty of Computer and Information Science CS 2 4 0 Pointers Self Study Exercises Model Answer 3. Write a function that returns a pointer to the maximum value of an array of double's. If the array is empty, return NULL. double* maximum(const double* a, int size); Your main function should be as follows: void main() { double a[]={10,50,14,20,94,27}; cout<<*maximum(a, 6)<<endl; } #include<iostream> using namespace std; double* maximum(double* a, int size) { double * max = a; for (int i=1; i<size; i++) if(*(a+i)>*max) *max = *(a+i); return max; } void main() { double a[]={44,50,14,20,94,27}; cout<<*maximum(a, 6)<<endl; } Page 5 of 5