Sunteți pe pagina 1din 14

ESc101: Fundamentals of Computing

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

Name: ____________________________ Section: ______ Roll No: ____________


Please Read the instructions carefully, before starting to answer the questions
1.
2.
3.
4.
5.
6.
7.
8.
9.

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.

Write appropriate declarations for the following.

a) character variable, tab, initialized to tab character


char tab = \t;
b) 1-dimension integer array, time, with two elements, initialized to 7 and 30 respectively.
int time [ ] = {7, 30};
c) floating point variable, pi, initialized to the value of PI (to 2 places of decimal).
float pi = 3.14;
d) A very large integer, bigint, initialized to four billion.
long int bigint = 4000000000;
e) A string called, city, initialized to name of our city, Kanpur.
char city [ ] = Kanpur;

Mid-Sem Exam, Esc101, Sep 14, 2011

Roll Number: __________________________, Section: _________________


Q. No.3.

Consider the following code for computing Ackermann function.

int ack(int m, int n)


{
if (m == 0) return (n+1);
if ((m > 0) && (n == 0)) return ack(m-1, 1);
if ((m > 0) && (n > 0)) return (ack (m-1, ack(m, n-1));
}
From the main function, a call is made to ack (1, 2). List all calls made to function ack,
in the sequence they are made, with values of m and n, and the return value of that call.
(Draw more rows of the table, if needed.)
Call

Return Value

ack (1, 2)

ack (1, 1)

ack (1, 0)

ack (0, 1)

ack (0, 2)

ack (0, 3)

Mid-Sem Exam, Esc101, Sep 14, 2011

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

long int f4 (int n)


// Recursive version

if (n == 1) return j;

if (
if (n == 1) return 1;

n>1

if (n > 1) return (n * f4 (n-1));

for (i = 2; i <= n ; i++)

j = j * i;
return j;
}
}

Mid-Sem Exam, Esc101, Sep 14, 2011

Roll Number: __________________________, Section: _________________


Q. No.5.

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
{

printf (Negative numbers not allowed); return ;

}
if (
{

n == 0

printf (%1d, n); return ;

}
printf (%1d,

n % 10 );

n = n / 10;
if (n > 0)

f5 (n);

Mid-Sem Exam, Esc101, Sep 14, 2011

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 ; )

printf (%d\n, i);


i = i + 1;
}

(b) int i;

for (i = 0; i <= 5; i++) ;


printf (%d\n, 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);

Mid-Sem Exam, Esc101, Sep 14, 2011

Roll Number: __________________________, Section: _________________


Q. No.7.

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

for (i = 0; i < len; i++)


{

cba

if (mark [i] ) continue;


str2 [size] = str1 [i];
mark [i] = 1;
size = size + 1;
if (size == len)
printf ("%s\n", str2);
else
f7 ();
size = size - 1;
mark [i] = 0;
str2 [size] = '\0';

}
}
void
{

main ( )

What does the program do, in general?


It is giving permutations of the characters in a
given string, assuming that all characters are
distinct.

int i;
/* scanf (%s, str1); */
for (i = 0; i < len; i++)
mark [i] = 0;
f7 ( );

Mid-Sem Exam, Esc101, Sep 14, 2011

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
{

check_board (int board [ ] [8])

int i, j;
int

rows [8],

columns [8];

for (i = 0; i < 8; i++)


{
rows [i] =

0 ; columns [i] =

0 ;

}
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
rows [i] = rows [i]

+ board [i][j];

columns [j] = columns [j]

+ board [i][j];

}
for (i = 0; i < 8; i++)
if ((rows [i]

!= 1) || (columns [i] != 1))

{
printf (Placement not proper\n); return ;
}
printf (Placement is proper\n);
}

Mid-Sem Exam, Esc101, Sep 14, 2011

Roll Number: __________________________, Section: _________________

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)

make_one_list (char names [ ][40], int

number)

{
char

onelist [820];

int

i;

onelist [0] = \0;


if (number > 0)
strcpy (onelist, names[0]);
for (i = 1; i < number; i++)
{
strcat (onelist, \n);
strcat (onelist, names[i]);
}
onelist [ strlen (onelist) + 1] = \0;
onelist [ strlen

(onelist) ] = \n;

printf (%s, onelist);


}

Mid-Sem Exam, Esc101, Sep 14, 2011

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

total_due (int initial, int yearly, int n, float rate)

{
int

deposit, i ;

int

j ;

// new line added only for quarterly interest

deposit = initial;
rate = rate / 4 ;

// new line added

for (i = 1; i < n; i++)


{
for (j = 0; j < 4; j++)

// new line added

deposit = deposit * (1 + rate/100);


deposit = deposit + yearly;
}
for

(j = 0; j < 4; j++)

// new line added

deposit = deposit * (1 + rate/100);


return deposit;
}

Mid-Sem Exam, Esc101, Sep 14, 2011

10

Roll Number: __________________________, Section: _________________


Q. No.11. You could find the area under the curve y = f(x) from x=x1 to x=xn in the following
method. You could divide the x1 to xn into several parts. For each part, the curve can be
approximated as a straight line. Hence a trapezoid is formed by lines x = x1, x = x2, y =
0, and the curve (approximated as the straight line) from y1 (= f (x1)) to y2 (= f (x2)).
The area of this trapezoid is (y1 + y2)*(x2 x1)/2. We can add these areas of all
trapezoids to get the total area under the curve. As the number of divisions increase, the
approximation of the area will keep getting better. We have written a program to do this
for the function f (x) = x*x. We are computing the area under the curve from x = 1.0 to
x = 3.0, by looking at 2 divisions and 4 divisions.
Go through the program and write down the values that will be printed by the program.
(You can approximate to 1st decimal place.)
float
{

f (float x)

return (x*x);

}
float part_area (float x1, float x2)

2.5

float area = (f(x1) + f(x2))* (x2x1) / 2;

6.5

printf (%f\n, area);

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:

for (j = 0; j < 4; j++)

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
{

printmatrix (int m[ ][6])

int i, j;
for (i = 0; i < 6; i++)
{

for (j = 0; j < 6; j++)


printf ("%d ", m[i][j]);
printf ("\n");

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

for (i = 0; i < 6; i++)

24

25

26

39

31

32

for (j = 0; j < 6; j++)

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

f12a (m); printmatrix (m); // P2


f12b (m); printmatrix (m); // P3
}

Mid-Sem Exam, Esc101, Sep 14, 2011

12

Roll Number: __________________________, Section: _________________

Rough Work

Mid-Sem Exam, Esc101, Sep 14, 2011

13

Rough Work

Mid-Sem Exam, Esc101, Sep 14, 2011

14