Midsem Answers
Midsem Answers
There are 6 questions in this exam. Write your answers directly on this paper in the
spaces provided.
Do not use a more general type where a simpler type would work (eg. do not use a float
type where int will work.)
Q1) [10 + 5 = 15 Marks] Consider the Peasants' Algorithm for multiplication of two positive
integers. It works in the following manner.
● Write the two numbers in two columns. Keep updating according to the following
procedure until the number in the first (i.e., left) column becomes 1.
● Halve the number in first column (integer division), double the number in the second
column.
● At the end, sum up all the numbers in the second column, for which, corresponding
number in the first column is odd.
1. 13 x 8:
13 8 ←
6 16
3 32 ←
1 64 ←
Answer: 64 + 32 + 8 = 104
(Note: This algorithm is related to binary multiplication procedure. It can be seen by writing both
the numbers in binary form and then applying standard multiplication.)
main_program{
int num1,num2;
cin>>num1>>num2;
int product = 0;
peasantMultiplication(num1,num2,product);
cout << product << endl;
}
(b). In the following two cases, what will be the final value of num_iterations when the function
peasantMultiplication returns ? [2.5 + 2.5 = 5]
1. peasantMultiplication(32,2): ___6___
2. peasantMultiplication(2,32): ___2___
Evaluation Instructions:
- Part (a) : Each blank carries 2 marks.
- Part (b) : Both the answers carry 2.5 marks each.
Q2) .[14 marks] Write the output of the code shown below.
#include <simplecpp>
int f(int x,int y)
{
if( x < y)
{
return x*x;
}
else return y*y*y;
}
int g(int a)
{
for(int i=0; i<=a; i++)
{
if(a == i*i*i)return true;
}
return false;
}
int h(int a)
{
for(int i=2; i*i<=a; i++)
{
if(a%i == 0)return true;
}
return false;
}
main_program{
int j=1;
int x=4;
for(x=0;x<12;x++)
{
if(h(x))continue;
j++;
cout<<f(x+1,j)<<" ";
if(g(j))break;
}
cout<<endl;
cout<<x<<" "<<j<<endl;
}
Answer:
1 4 9 16 216 343 512
11 8
Evaluation instructions:
1 4 9 16 : For first 4 numbers in line 1: 4 marks, marks will be awarded only when all the
4 numbers match, else awarded 0 marks.
216 343 512: For next 3 numbers in line 13: 4 marks, marks will be awarded only when
all the 3 numbers match, else awarded 0 marks.
11 8: 6 marks. 3 marks for each correct number. Partial marks will be given here if 1 of
the 2 blanks is correct.
Q3). [15 marks]
The source code for two programs series1.cpp and series2.cpp are given as follows. The code
for series2.cpp contains a few blanks.
(a). Complete the blanks with valid C++ expressions (consisting of constants, variables and/or
arithmetic operators used in the correct syntax of C++) such that both the programs print the
same output, given the same input. You are not allowed to use any math functions like sqrt() or
pow(). [12 marks = 3+ 6+ 3]
[series1.cpp]
#include <simplecpp>
main_program {
double z;
cin >> z
double sum = 0, term1 = 0, term2 = 1, term3 = z;
main_program {
double z;
cin >> z;
double sum = 0;
(b). Write the invariants for variables term2, term3 and sum at the start of the loop?[1+1+1
marks]
Answer:
1) term2 stores (i-1)! (i-1 factorial) 1 mark (no partial marking)
2) term3 stores z^i (z power i) 1 mark (no partial marking)
3) sum stores sum of (i-1) terms of the series. 1 mark (no partial marking)
Evaluation Instructions:
Part (a).
3 marks - blank 1
6 marks - blank 2
3 marks - blank 3
If the expression is mathematically correct and not a valid C++ syntax, half the marks will
be awarded.
Marks will be awarded only if the complete blank is matched, else award 0.
Part (b).
Note: For this part, c++ syntax need not be followed
Q4)
[8 + 8 = 16 marks]
Using recursion helps in avoiding duplication of code and makes the code easier to write. In this
question, we try to verify it by writing iterative and recursive code for the following image.
Consider the functions drawSquareIter and drawPatternRec. Both create the same final
image. drawSquareIter is an iterative function whereas drawPatternRec is a recursive one.
(a). Following is the incomplete code for the function drawSquareIter. Fill in the blanks. Also,
what should be the values of X and Y? [8 Marks]
#include <simplecpp>
void drawSquareIter(double side){
repeat(X){
repeat(X){
repeat(X){
repeat(X){
forward(0.064*side);
right(Y);
}
forward(0.16*side);
right(Y);
}
forward(0.4*side);
right(Y);
}
forward(side);
right(Y);
}
}
Value of X = 4
Value of Y = 90
(b). Now, following is the incomplete code for function drawPatternRec. To draw the given
figure, the function will be called with level=4. Value of sideLen will be the length of outermost
square. [8 Marks]
repeat(4){
drawPatternRec(0.4*side,level-1);
forward(side);
right(90);
}
}
Evaluation instructions:
- Part (a). marks distribution:
- Value of X : 1 mark
- Value of Y : 1 mark
- All other blanks : 2 marks each
- Part (b). marks distribution:
- All blanks : 2 marks each
Q5). [14 Marks] The Tower of Hanoi is a puzzle which consists of three rods, and a number of
disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat
stack in ascending order of size on one rod, the smallest at the top, thus making a conical
shape. The objective of the puzzle is to move the entire stack from first rod to the third rod,
obeying the following simple rules:
The above figure is a solution for the Tower of hanoi puzzle consisting of two disks.
E.g: Below are the sequence of steps to solve for a tower consisting of 2 disks.
Move the top disk from rod-1 to rod-2
Move the top disk from rod-1 to rod-3
Move the top disk from rod-2 to rod-3
main_program{
int n;
cin >> n;
solve_tower_of_hanoi(n,1,2,3);
}
(a). Draw the call tree for the function call solve_tower_of_hanoi(4,1,2,3). Call tree for the
function call solve_tower_of_hanoi(2,1,2,3) is given below, where the name func has been
used to indicate the function solve_tower_of_hanoi for brevity.
Note that the left function call represented by the left node executes completely before the right
function call. This property should be preserved by your solution.
Answer:
Evaluation Comments :
(a) 10 marks - 1 mark penalty for each incorrect node. Minimum marks
awarded is 0.
Q6). [Marks 14] Given below are two recursive functions to calculate the value of nCr. nCr is
the number of ways of selecting r items given n items.
n n
The recurrence for calculating Cr is given by: Cr = n-1Cr + n-1Cr-1.
Assume that n>=r in this question.
The function ‘nCr_erroneous’ has some errors in it and the function ‘nCr_correct’ has some
missing parts. Read the code below and proceed to answer some questions below.
Code:
int nCr_erroneous(int n, int r) {
if(r==0)
return n;
else if(n<=r)
return n;
return nCr_erroneous(n-1,r+1) + nCr_erroneous(n-1,r-1);
}
(a). Give the return value of the nCr_erroneous function for the following function calls
nCr_erroneous(2,0): 2
nCr_erroneous(2,2): 2
nCr_erroneous(5,3): 11
Evaluation Instructions:
Part (a): 2 marks per correct answer[2*3 = 6].
Part (b): 2 marks for the first two blanks and 1 mark each for other four
blanks[2*2 + 4 = 8]
Note that the two recursive calls may be written in different order.