# sheet 13pointers by HC120620232245

VIEWS: 0 PAGES: 4

• pg 1
Princess Nora Bint Abdulrahman University       CS 240 - Computer Programming 1
College of Computer and Information Sciences    Sheet # 13 (Pointers)
Department of Computer Sciences                 2nd Semester 1432-1433H
Theoretical Part:
Choose the correct output:
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.

1
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;
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

7.
string s;
int* i;
s = “Fred Jones”;
i = &s;
cout << *i;
a) Fred Jones

2
b) A garbage number
c) Won't run

8.
void doubleref(int* p) {(*p) = (*p) * 2;}
void main()
{
int a = 5;
doubleref(&a);
cout << a;
}
a) 5
b) 10
c) Won't work

3
Programming part:

1. Write a function countEven(const int* arr, int size)
which receives an integer array and its size, and returns the number of
even numbers in the array.

2. Write a function myStrLen(char*) which returns the length of
the parameter cstring. Write the function without using the C++
function strlen.

3. Write a function contains(char*, char) which returns true if
the 1'st parameter cstring contains the 2'nd parameter char, or false
otherwise.

4. Write a function revString(char*) which reverses the
parameter cstring. The function returns nothing. You may use C++
string handling functions in <cstring> in the function if you wish.

4

To top