You are on page 1of 4

ESC112A Quiz-1

Name ..................................
Jan 3, 2023
Duration 30 Minutes
Max Marks: 20 Roll no. ..................................
Closed Book and
Closed Notes
Section .....................................

Instructions

1. The exam is closed book and closed notes. Use of mobile phones or access to
internet is not allowed.
2. This quiz paper has two sheets or four pages. There are a total of three questions.
3. Write your name, roll no. and section on both this and the third page top, in the
spaces provided.
4. Write your nal answers neatly in the space provided, strike out any rough work.
Separate space for rough work is provided after each question.
Q1 (points 7) For a given problem, there may be more than one way to express its
solution on a given input, in terms of its solution on smaller inputs. For computing
power(x, n) we saw one recursive decomposition in the lectures, below we outline a
dierent recursive decomposition of the same problem.

/* n>= 1 */
double power(double x, int n){
double y, z;
if(n==1) return x;
else {y = power(x, _______1________);
z = n%2==0 ? _______2________ : _______3________;
return y*y*z;
}
}

In answers to parts (a), (b) and (c) below, write the expressions that should be lled
in places 1, 2 and 3 respectively so that the function works correctly. Your answer
expressions should not contain any function calls.
(a)

(b)

(c)

-Rough Work Below this line

2
Name ..................................

Roll no. ..................................

Section .....................................

Q2 (points 6) Consider the code below similar to binRep function of lectures.

/* x >=0 */
void binaryRep(int x){
if (x==0) printf("%d", 0);
else {binaryRep(x/2);
printf("%d", x%2);
}
}

In the parts below, ll in the output of binaryRep when x is as given.


(a) x = 57
Output: .........................

(b) x = 255
Output: .........................

Rough Work Below this line

3
Q3 (points 7) In the factorize function of lectures, after nding a factor k, we search for
the next factor again from 2 onwards. This is wasteful, we know that the next factor
is ≥ k, it suces to search it from k onwards. Following is the outline of modied
factorize function which intends to do this. It uses an auxiliary function
factorize_1, with an extra parameter i.

/* x >1.
prints prime factors of x in nondecreasing order */
void factorize(int x){
factorize_1(x, 2);
}

void factorize_1(int x, int i){


while(i<x && _______1________)i++;
if(_______2________)
{printf("%d ", i);
factorize_1(x/i, _______3________);
}
else printf("%d", x);
}

In answers to parts (a), (b) and (c) below, write the expressions that should be lled
in places 1, 2 and 3 respectively so that the function works correctly.
(a)

(b)

(c)

Rough Work Below this line 

You might also like