You are on page 1of 8

Florence Chan

Winter 2015

Review Questions

Question 1.

[12 marks]

Sub-questions are based on the following code snippet:


int i=1;
int *p;
int **q;
Assume that the following assignments follow immediately after the above code snippet. Comment whether
these assignments are valid or not. If they are valid, then what are the values stored in the associated
variables? Use pointer diagrams to validate answers.

Part (a) [1 mark]


*p=1;

not valid

Part (b) [1 mark]


p=&i;

valid
p is the memory address of the integer variable i

Part (c) [1 mark]


q=1;

not valid

Part (d) [1 mark]


q=p;

not valid

Part (e) [1 mark]


q=&p;

valid
q is a double pointer and it points to the memory address of pointer p

Page 1 of 8

CSC 190 H1S

Review Questions

Part (f ) [1 mark]
p=&i;
*p=1;
printf("\%d\n",i);

valid; p points to the memory address of the integer variable i


dereferencing p to 1 which means i = 1
prints out: \1

Part (g) [1 mark]


q=&p;
*q=1;

valid; q points to the memory address of pointer p


not valid because p is a pointer; dereferencing double pointer

Part (h) [1 mark]


q=&p;
**q=1;
printf("\%d\n",i);

valid; same as above


valid; i =1, dereferencing
prints out: \1

Part (i) [1 mark]


*&p=i;

not valid

Part (j) [1 mark]


*&p=&i;

valid *& cancels

Part (k) [1 mark]


&*p=i;

not valid

Page 2 of 8

Winter 2015

Review Questions

Part (l) [1 mark]


&*p=&i;

not valid
the address of i = address of i

constant pointer
cannot modify
contents

Question 2.

[2 marks]
Examine the following code snippet and comment on its validity. If there is a problem, what is it?
void f(int *i)
{
int p=*i;
printf("%d\n",p);
}
int main()
{
int k;
f(&k);
}

it would run without errors but I dont think it will give the
output that you are expecting. cause pointers are %p
invalid address
before assignment
haven;t set k to
anything

need to assign k to
initialize and
therefore point to
the address

Question 3.

[2 marks]
Examine the following code snippet and comment on its validity. If there is a problem, what is it?
void f(int *i)
{
*i=3;
return *i;
}
int main()
{
int k;
f(&k);
printf("\%d\n",*k);
}

not valid because its a void type but returns something

Question 4.

[2 marks]
Examine the following code snippet and comment on its validity. If there is a problem, what is it?
int* f(void)
{
int i=3;
return &i;
supposed to return a pointer not the memory address but still valid since p = &i
}
int main()
{
int *k;
k=f();
prints 3; what's with the extra \
printf("\%d\n",*k);
}
Page 3 of 8

CSC 190 H1S

Question 5.

Review Questions
[2 marks]

Examine the following code snippet and comment on its validity. What needs to be freed? What are the
contents of the freed elements?
int* f(void)
{
int * i=(int *) malloc(sizeof(int));
int *p=i;
*i=1;
return i;
}

Question 6.

free i?

free i or p because
they point to the
same thing anyway

[2 marks]

What will be printed to the screen?


void f(int *p, int n)
{
p[n-1]=2;
}
int main()
{
int array[]={1,2, 3, 5};
f(array, 4);
printf("%d\n", array[3]);
}

Question 7.

[2 marks]

Change this function implementation so that there is no need for a return value (i.e. f can be changed to
type void) but the result is indirectly passed to the function callee. Feel free to add more arguments.
int f(int p)
{
p=p+2;
return p;
}

Question 8.

pointers?
pointer points to &p
another argument thats a pointer

[2 marks]

What will be printed to the console?


int main()
{
int array[]={1,2, 3, 5};
printf("%d\n", *array);
}

Page 4 of 8

void type
F (int *p)
*p = *p +2

Winter 2015

Review Questions

Question 9.

[2 marks]

What values are stored in the memory regions pointed to by a and b?


void f(int *a, int *b)
{
int i=*a;
*a=*b;
*b=i;
}

Question 10.

none yet...

A stores B's value and B stores A's


value

[2 marks]

What entities must be freed to avoid a memory leak?


int * f(int **a)
**c
{
int *i=(int *)malloc(sizeof(int));
int *j=(int *)malloc(sizeof(int));
*i=1;
*j=2;
*a=j; *c = j
return i;
}
int main()
{
int *b;
int *a=f(&b);
}

Question 11.

free whatever was dynamically


i and j? allocated
in this case a and b

[2 marks]

What happens to the region allocated to p when a is freed and vice versa?
int * f(void)
when you free one
{
iti is gone
int *i=(int *)malloc(sizeof(int));
return i;
}
main | f |
when a or p is freed this disappears
int main()
p
{
||
i
[]
int *p=f();
a
int *a=p;
}

Page 5 of 8

CSC 190 H1S

Question 12.

Review Questions
[2 marks]

What is the difference between a in both of these functions? Which one is a valid function?
int * f(void)
{
int *a=(int *)malloc(sizeof(int));
*a=3;
return a;
}
int * g(void)
{
int i;
int *a=&i;
*a=3;
return a;
}

Question 13.

[2 marks]

What are the problems in these functions?


void f(void)
{
int *a=(int *)malloc(sizeof(int));
g(&a);
free(a);
}
void g(int **a)
{
*a=(int *)malloc(sizeof(int));
}

Question 14.

[2 marks]

What are the problems in these functions?


int f(double a)
{
return a*a;
}
void g((int)f(int), int a)
{
printf("%d\n",(*f)(a));
}
int main(void)
{
g(f,8);
}

Page 6 of 8

Winter 2015

Review Questions

Question 15.

[4 marks]

Iteratively (without using recursion) implement multiplication of two numbers by expanding the function
int iMultiply(int n, int m) where n is the left operand and m is the right operand of the multiplication
operation by converting the multiplication problem into a summation problem as follows. Suppose that
you wish to compute a b. This is equivalent to adding a b number of times. For instance, 4 2 = 4 + 4.
This function will return the computed result.

Question 16.

[5 marks]

Recursively implement multiplication by expanding the function int rMultiply(int n, int m). Convert
the original multiplication problem into a summation problem as per the logic provided in the previous
example.

Question 17.

[5 marks]

Compute the resulting value when the number base is raised to the power exp by implementing the function
int power(int base, int exp) recursively. This function will return the computed result. Draw the
call tree for the specific instance power(2,3).

Question 18.

[5 marks]

Implement int halfpower(int base, int exp) recursively but this time separate the problem into
halves at each recursive call. Ensure to take into account odd and even values of exp. Draw the call
tree for the specific instance power(2,3).
C(n, 0) = 1 and C(n, n) = 1
C(n, k) = C(n 1, k) + C(n 1, k 1)

Page 7 of 8

if n 0

(1)

if n > k > 0

(2)

CSC 190 H1S

Review Questions

Question 19.

[5 marks]
Implement the Ackermanns function by expanding the function int A(int m, int n). This function will
return the computed result. The Ackermanns function is defined as follows:
A(m, n) = n + 1
A(m, n) = A(m 1, 1)
A(m, n) = A(m 1, A(m, n 1))

if m = 0

(3)

if n = 0 and m > 0

(4)

if m>0 and n>0

(5)

Question 20.

[5 marks]

The binomial function nk can be implemented recursively via this formula:
C(n, 0) = 1 and C(n, n) = 1
C(n, k) = C(n 1, k) + C(n 1, k 1)

if n 0

(6)

if n > k > 0

(7)

Implement the function int binomial(int n, int k) which returns the computed result.

Question 21.

[6 marks]
The Newtons method can be applied recursively to compute the approximate square root a of a number
x that is precise up to eps of the exact root. Implement the function double newtonSqrt(double x,
double a, double eps). The Newtons square root algorithm is as follows. Suppose that you would like
to compute the square root of x. Set the first guess of the solution to be a=x/2. Compute |a a x|. Check
if this is less than eps. If it is, then a is the root that is at least eps from the exact answer. Otherwise,
modify your current guess a to a = (a + x/2)/2 and repeat the above algorithm. This function will return
the computed result.

Question 22. [6 marks]


In this question, you will implement the function int findMinChangeCoins(int C, int n). This function shall use a recursive technique to find the minimum number of coins needed to make a given amount of
change. Suppose, that the currency system we are considering consists of coins with values S={1, 5, 10,
20, 25}. Consider the specific scenario where we need to make a change for 40 cents. A greedy algorithm
may suggest that the coins needed for making this change are 25 cents, 10 cents and 5 cents (3 coins).
However, we only need 2 coins (two 10 cent coins). Implement the function that computes the minimum
number of coins needed to break a change by expanding the function int findMinChangeCoins(int C,
int n) where C is the change needed, n is nth coin in the set S containing the denominations of the coins.
Assume that an infinite number of coins in all denominations are readily available. This function will
return the computed result.
Page 8 of 8

You might also like