Académique Documents
Professionnel Documents
Culture Documents
2011-12-Monsoon Semester
Mid-semester Examination (SOLUTIONS)
Wednesday, September 14, 2011
Time: 2 hours (07:30 AM to 09:30 AM)
Maximum Marks: 60
Please write your name, roll number, and section in space provided above.
Please write your roll number and section on each odd page of the question paper.
The question paper contains 12 questions in 14 pages (including cover sheet, and rough sheets).
You should not be keeping any reading material in your vicinity.
You should not have carried your mobile phone to the exam hall as per the Institute policy on
use of mobiles. But if you have, keep that in front of the lecture hall. We do not take any
responsibility of any item kept in the front of the lecture halls.
If you are found carrying a mobile during the exam, even in switched off mode, it will be
assumed that you had intended to use that mobile for unfair means and dealt with in accordance
with the course policy on academic integrity.
Any act of academic dishonesty will result in failure in the course.
Please write legibly. The graders are human. They cannot guess what you intended to write.
They will give marks on what they perceive you have written.
You SHALL NOT turn this sheet until the bell goes.
Question
Max
Question
10
11
12
Total
Max
60
Score
Score
Q. No.1.
For each of the following names, please write down whether they are valid (V) or
invalid (I) names of the user defined functions in a C program.
Part
Answer (V/I)
a) int
a)
b) array
b)
c) function
c)
d) isaleapyear
d)
e) bool
e)
f) 4thelement
f)
g) 2times
g)
h) real
h)
i) character
i)
j) double
j)
Q. No.2.
Return Value
ack (1, 2)
ack (1, 1)
ack (1, 0)
ack (0, 1)
ack (0, 2)
ack (0, 3)
Q. No.4.
Consider the following code for a recursive function. Complete the code for the
equivalent iterative function. (There are five spaces to fill up.)
long int
f4 (int n)
// Iterative version
{
int
long int j = 1
if (n == 1) return j;
if (
if (n == 1) return 1;
n>1
j = j * i;
return j;
}
}
Consider the following code for an iterative function. Complete the code for the
recursive function, which gives the same output. There are five blank spaces to fill.
void
f5 (int n)
// Iterative version
{ int m = 0;
if (n < 0)
{ printf (Negative numbers not allowed);
return ;
}
while (n > 0)
{
m = (m * 10) + (n % 10);
n = n / 10;
}
printf (%d, m);
}
void
f5 ( int
n)
// Recursive version
{
if ( n < 0
{
}
if (
{
n == 0
}
printf (%1d,
n % 10 );
n = n / 10;
if (n > 0)
f5 (n);
Q. No.6.
What do the following program segments print? (Make more rows in the answer area, if
needed. The newline should result in writing the output number in the next row.)
(a) int i = 0;
for ( ; i<=2 ; )
(b) int i;
(c) int i;
for (i = 0; ; i++)
if (i > 8) break;
printf (%d\n, i);
(d) int i = 0;
for ( ; ; )
10
{
i = i + 1;
if (i < 10) continue;
else break;
}
printf (%d\n, i);
Consider the following program. What is the output of this program? What does this
program do, in general, if we had not initialized str1, but read it using scanf instead?
#include <stdio.h>
char
str1[10] = "abc";
char
str2[10] = abc;
int
mark[10];
abc
int
len = 3, size = 0;
acb
void
f7 ( )
Output of Program
bac
bca
int i;
cab
cba
}
}
void
{
main ( )
int i;
/* scanf (%s, str1); */
for (i = 0; i < len; i++)
mark [i] = 0;
f7 ( );
Q. No.8.
Consider a board game, where the board is of size 8x8, that is, there are 8 rows and 8
columns in the board, giving a total of 64 locations. We need to place 8 pieces on the
board in such a way that in any given row, there is at most one piece. Similarly, in any
given column, there is at most one piece.
A board is represented by a matrix, board [8][8]. If on a particular location, a piece is
being placed, it amounts to setting the value of that matrix element to 1. Other elements
in the matrix are going to be 0. Given below is an incomplete function that checks
whether the placement of pieces are correct or not. You need to fill eight spaces in five
lines of the program to complete it.
void
{
int i, j;
int
rows [8],
columns [8];
0 ; columns [i] =
0 ;
}
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
rows [i] = rows [i]
+ board [i][j];
+ board [i][j];
}
for (i = 0; i < 8; i++)
if ((rows [i]
{
printf (Placement not proper\n); return ;
}
printf (Placement is proper\n);
}
Q. No.9.
In a program, there is an array of strings (array of array of characters), names [20] [40].
This contains at most 20 names, each name having a maximum length of 39 characters.
We assume that each name is already being terminated by a null character. We are
interested in copying all the names from this 2-dimensional array to a large 1dimensional array, such that if we were to print that 1-dimensional array as a string
(using %s format), it will print different names on different lines. Part of the code to do
this is written, but six blank spaces have been left for you to fill in. They all correspond
to function calls.
(void)
number)
{
char
onelist [820];
int
i;
(onelist) ] = \n;
Q. No.10. A depositor in a bank sets up an account on 1st January with an initial deposit. She gets
an annual rate of interest, rate. She puts in an additional yearly deposit in the account on
every subsequent 1st January. After n years, she goes to the bank not to deposit the
yearly amount, but to close the account, instead. The bank used the following given
program to determine the total amount due to her.
If the bank was computing interest due every three months, and adding that to the
account, then the program would have been slightly different. You should fill up the
blank spaces (four of them) to convert this program into the one which compounds
interest every three months.
int
{
int
deposit, i ;
int
j ;
deposit = initial;
rate = rate / 4 ;
(j = 0; j < 4; j++)
10
f (float x)
return (x*x);
}
float part_area (float x1, float x2)
2.5
6.5
9.0
return area;
}
float total_area (float x1, float x2, int parts)
{
int i;
float x3, x4, area = 0.0;
for (i = 0; i < parts; i++)
0.8
1.6
2.6
3.8
8.8
{
x3 = x1 + i*(x2-x1)/parts;
x4 = x1 + (i+1)*(x2-x1)/parts;
area = area + part_area (x3, x4);
}
return (area);
}
int main ( )
{
printf ("%f\n", total_area (1.0, 3.0, 2));
printf ("%f\n", total_area (1.0, 3.0, 4));
}
Mid-Sem Exam, Esc101, Sep 14, 2011
11
Q. No.12. Consider the following program. What would be the output of this program?
void
{
f12a (int
m[ ][4])
int i, j;
for (i = 0; i < 6; i++)
Output of P1:
m[i][j] = i*6 + j;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
}
void
{
f12b (int
m[ ][3])
int i, j;
for (i = 0; i < 6; i++)
for (j = 0; j < 3; j++)
m[i][j] = i*6 + j;
}
void
{
int i, j;
for (i = 0; i < 6; i++)
{
Output of P2:
0
12
13
14
15
18
19
20
21
24
25
26
27
30
31
32
33
24
25
26
27
28
29
30
31
32
33
34
35
printf ("\n");
}
Output of P3:
int main ( )
{
int i, j;
int m[6][6];
12
13
14
18
19
20
24
25
26
39
31
32
26
27
30
31
32
33
m[i][j] = i*6 + j;
24
25
26
27
28
29
30
31
32
33
34
35
printmatrix (m);
// P1
12
Rough Work
13
Rough Work
14