Professional Documents
Culture Documents
Winter 2015
Review Questions
Question 1.
[12 marks]
not valid
valid
p is the memory address of the integer variable i
not valid
not valid
valid
q is a double pointer and it points to the memory address of pointer p
Page 1 of 8
Review Questions
Part (f ) [1 mark]
p=&i;
*p=1;
printf("\%d\n",i);
not valid
not valid
Page 2 of 8
Winter 2015
Review Questions
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);
}
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
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]
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]
Page 4 of 8
void type
F (int *p)
*p = *p +2
Winter 2015
Review Questions
Question 9.
[2 marks]
Question 10.
none yet...
[2 marks]
Question 11.
[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
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]
Question 14.
[2 marks]
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)
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)
(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.