IITK Midsem With Soln
IITK Midsem With Soln
01
Instructions:
1. Write your name, roll number and section in the space above.
2. On the top of every odd page write your roll number.
3. Answer all questions in the space provided in this paper itself. No extra sheet will be provided.
4. Use the designated spaces and the last sheet for rough work.
5. Marks for every question is shown with the question.
6. Questions have been shuffled across different question papers.
Answer: 1011100.10100011
(b) Convert 209.375 (in decimal system) to the hexadecimal system. [2 Marks]
Answer: d1.6
1
(c) Represent 53 and −69 in binary 8-bit 2’s complement representation. [1 + 1 = 2 Marks]
53 =
-69 =
Answer:
53 = 00110101
-69 = 10111011
(d) The following expression has a single pair of parentheses missing that makes it syntactically wrong and it
does not compile. Place a pair of parentheses in proper places to correct the expression and evaluate it
for x = 2, y = 3, z = 5, w = 7: [2 + 1 = 3 Marks]
x * w + y * z = 7
Corrected Expression:
Answer:
Corrected Expression: x * w + y * (z = 7)
Value of the corrected expression: 35
2
Roll No:
For i = 2, j = ______
For i = 4, j = ______
For i = 1, j = ______
Answer:
For i = 2, j = 256
For i = 4, j = 256
For i = 16, j = 256
For i = 1, j = 1
int main() {
int w = 0, x = 2.5, y = 5, z = 3, r, s = 4, t = 5, u = -3;
double a = 2.36, b = 3.19, c = 3.0, d = 2.91726;
Expr_1 = __________
Expr_2 = __________
Expr_3 = __________
Answer:
Expr_1 = 8
Expr_2 = 65.000000
Expr_3 = 1.000000
3
(g) Write the output of the following program. [0.5 + 0.5 = 1 Mark]
#include <stdio.h>
#define m 5+5
const int n = 5+5;
void main() {
int a = 0, b = 0;
a = m * m;
b = n * n;
printf("%d %d\n", a, b);
}
Answer:
35 100
(h) You need to convert the following for-loop into an equivalent while-loop: [0.5 + 0.5 + 0.5 + 0.5 = 2
Marks]
for(i = 2; i <= sqrt(n); i += 3)
printf("%d ", i);
Fill up the blank lines.
____________________;
while (____________________) {
____________________;
____________________;
}
Answer:
i = 2;
while (i <= sqrt(n)) {
printf("%d ", i);
i += 3;
}
4
Roll No:
(i) What will be the output from the following program? [2 Marks]
#include <stdio.h>
void f(int a[], int n)
{
int i;
for (i = 0; i < n - 1; ++i)
a[i] += a[i+1];
}
int main ()
{
int a[5] = {1, 2, 4, 6, 8};
f(a, 4);
printf("%d", a[4] - a[3]);
return 0;
}
Answer: 2
(j) What will be the output from the following program? [1 + 1 + 1 + 1 = 4 Marks]
#include <stdio.h>
int main()
{
int i = 1, j = 1, k = 1, count = 0;
while (i < 2) {
for(; j < 4; j += k)
do {
++count;
k += i;
} while (k < 8);
i += j;
}
return 0;
}
5
Answer:
Loop Indices: 10 9 8
Number of iterations = 7
void main() {
int a[n];
int i;
a[0] = 1;
for(i = 0; i < n - 1; ++i) {
a[a[i]] = a[i]+1;
}
Answer:
1 2 3 4 5
#include <stdio.h>
void main() {
int a[] = {22, 19, 17, 36, 12, 15, 28, 35, 66, 43};
int i, j, n = sizeof(a)/sizeof(int);
6
Roll No:
Answer:
66 43 36 35 28 22 19 17 15 12
#include <stdio.h>
int main() {
unsigned int nMax = 16, sum = 0, n = 0;
(a) Compute h(n) for n = 2, 5, and 7 to get an idea for what the function h(n) does. Describe h(n) in
words for a given n. [1 + 1 + 1 + 1 = 4 Marks]
h(2) = _____
h(5) = _____
h(7) = _____
h(n) = ____________________________________________________________
7
Answer:
h(2) = 1
h(5) = 2
h(7) = 3
h(n) = number of 1’s in the binary representation of n.
Answer:
Sum = 32
(c) What will be the output of the program if nMax is initialized to 2K for some K > 0? [3 Marks]
Answer:
K2K−1
4. The following program intends to compute π by Newton’s formula upto the 10000-th term:
∞
π X 2k (k!)2
=
2 (2k + 1)!
k=0
Fill up the dashed lines in the program. [0.5 + 0.5 + 0.5 + 3 + 0.5 = 5 Marks]
Note: Since factorial of a number cannot be computed in a single expression, you need to find an iterative
formula to compute the k-th term tk from the (k − 1)-st term tk−1 .
8
Roll No:
#include <stdio.h>
int main ()
{
double sum = _____;
int k = _____;
term = ________________________________________;
sum = ____________________;
}
sum *= 2;
printf("%lf", sum);
return 0;
}
Answer:
#include <stdio.h>
int main_pi()
{
double sum = 1.0;
double term = 1.0;
int k = 1;
printf("%lf", sum);
return 0;
}
9
5. The following function Solve() is designed to solve a quadratic equation ax2 + bx + c = 0. Hence it takes
three floating-point (real) coefficients as input parameters a, b & c, and generates up to two roots (wherever
possible) of the equation. Solve() sets the roots in the global variables r1 & r2. Further Solve() returns a
status value retVal to describe the type of the solution computed. The status can take the following values:
10
Roll No:
r1 = _________________________;
r2 = _________________________;
}
} else {
double disc = b*b - 4*a*c;
if (0 == disc) {
retVal = _________________________;
r1 = _________________________;
r2 = _________________________;
} else {
if (disc > 0) {
retVal = _________________________;
r1 = _________________________;
r2 = _________________________;
} else { // Complex conjugate roots
retVal = Sol_ComplexRoots;
// r1 and r2 need not be set in this case
}
}
}
return retVal;
}
11
Answer:
return retVal;
}
12
Roll No:
6. Consider the following C structure for representing a vector ~v = (vx , vy ) in two-dimensional Cartesian co-
ordinate system:
#include <stdio.h>
#include ____________________
double norm(Vector v) {
return ________________________________________;
}
The following function computes the difference vector (~v1 − ~v2 ) of two vectors ~v1 and ~v2 :
v.x = ____________________;
v.y = ____________________;
return v;
}
Answer:
#include <stdio.h>
#include <math.h>
--------
double norm(Vector v) {
return sqrt(v.x*v.x + v.y*v.y);
-----------------------
}
13
(b) Fill up the gap below to compute the Euclidean distance between two points p and q by suitably calling
the functions norm(v) and diff(v1, v2): [2 Marks]
Vector p, q;
double dist;
// ...
Answer:
(c) The following function takes a vector ~v = (vx , vy ) as input and outputs its reflection ~vr = (−vx , −vy )
about the origin (0, 0).
______________ reflect(Vector v) {
Vector vr;
______________________________;
______________________________;
return vr;
}
Complete the function [1 + 0.5 + 0.5 = 2 Marks]
Answer:
Vector reflect(Vector v) {
------
Vector vr;
vr.x = -v.x;
-----------
vr.y = -v.y;
-----------
return vr;
}
14