Nothing Special   »   [go: up one dir, main page]

CL1 Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

INDIAN INSTITUTE OF TECHNOLOGY

KHARAGPUR
Stamp / Signature of the Invigilator

EXAMINATION ( Class Test - 1 ) Full Marks - 30

Roll Number Section Name

Subject Number C S 1 0 0 0 1 Subject Name Programming and Data Structures

Department / Center of the Student Additional sheets

Signature of the Student

To be filled in by the examiner

Question Number 1 2 3 4 5 6 7 8 9 10 Total

Marks Obtained

Marks obtained (in words) Signature of the Examiner Signature of the Scrutineer

Answer all the Questions. Write the answers in the boxes only. You can use
the designated spaces for rough works.

1. Determine the normalized number from the given single precession floating point representation. The
normalized number is represented as (−1)s × 1.(mantissa) × 2exponent . Determine each component
and show the number in the format mentioned above. [4]

10111010101101111010000000000000

2. Answer the following questions: [9]

(a) (2 marks) Write a condition which will be true if any pair of the int variables x,y,z are equal
to each other, false otherwise.

(b) Consider the following program:


#include <stdio.h>
int main() {
char a=’s’-’g’,b=’U’-’L’;
int c;
scanf("%d",&c);
if(c>a) printf("First condition\n");
if(c<b) printf("Second condition\n");
else printf("Third condition\n");
}

(2 marks) Write the output if the input value of the variable c is 3.

(c) (3 marks) In the above program, what is the range of input c for which the output is “Third
condition”?

(d) (2 marks) Write the output of the program for input value of the variable c = 2.

#include <stdio.h>
int main() {
int c;
scanf("%d",&c);
switch(c) {
case 2-1: printf("One\n");
case 1+1: printf("Two\n");
default: printf("Default\n");
}}

3. What will be printed by the following program? [5]

#include <stdio.h>
int main() {
int a = 10, b = 3, c, d, i = 20, j = 30, x;
x = a / b;
c = x++;
d = x / c;
d++;
i *= ++c;
j /= i++;
printf("x = %d c = %d d = %d i = %d j = %d", x, c, d, i, j);
return 0;
}

2
4. The following code computes the prime factorization of a number a (≥ 2) given as input. For example,
if a = 72, it will print: Prime factorization = 2 x 2 x 2 x 3 x 3. Fill up the blanks
in this code. [5]

#include <stdio.h>
int main(){
int a, d = 2;
printf("Enter a positive integer: ");
scanf("%d", &a);
printf("Prime factorization = ");
do{
if (_____________){ // 1.5 marks
printf("%d x ", d);

______________; // 2 marks
}
else
______________; // 1.5 marks
}
while(a >= d);
printf("\b\b \b\b.\n");
return 0;
}

5. For the C program given below, what will be the output for the input 1234 5678 f (three data items
with two spaces in between) are given? [5]

#include <stdio.h>
int main() {
int a,b;
char c;
printf("Enter the values of a, b, c:");
scanf("%2d%3d%c",&a,&b,&c);
printf("a = %d\n b = %d\n c = %c\n",a,b,c);
scanf("%2d%3d%c%c",&a,&b,&c,&c);
printf("a = %d\n b = %d\n c = %c\n",a,b,c);
return 0;
}

3
6. What is the output of the following program? [2]

#include <stdio.h>
int main() {
double x = 123456789.99;
int y = x;
printf("y = %d\n y = %0.2lf\n", y, (double)y);
return 0;
}

Space for Rough Work

4
1. Determine the normalized number from the given single precession floating point representation. The
normalized number is represented as (−1)s × 1.(mantissa) × 2exponent . Determine each component
and show the number in the format mentioned above. [4]

10111010101101111010000000000000

Ans:
Sign bit = 1
Mantissa = 01101111010000000000000 (last 23 bits)
Exponent = 117 - 127 = -10
Normalized Number = (−1)1 × 1.0110111101 × 2−10

2. Answer the following questions: [9]

(a) (2 marks) Write a condition which will be true if any pair of the int variables x,y,z are equal
to each other, false otherwise.

((x==y)||(y==z)||(x==z))

(b) Consider the following program:

#include <stdio.h>
int main() {
char a=’s’-’g’,b=’U’-’L’;
int c;
scanf("%d",&c);
if(c>a) printf("First condition\n");
if(c<b) printf("Second condition\n");
else printf("Third condition\n");
}

(2 marks) Write is the output if the input value of c is 3.

Second condition

(c) (3 marks) In the above program, what is the range of input c for which the output is “Third
condition”?
9 − 12

(d) (2 marks) Write the output of the program for input value of the variable c = 2.

#include <stdio.h>
int main() {
int c;
scanf("%d",&c);
switch(c) {
case 2-1: printf("One\n");
case 1+1: printf("Two\n");
default: printf("Default\n");
}
}

Output for c = 2: Two


Default

5
3. What will be printed by the following program? [5]

#include <stdio.h>

int main() {
int a = 10, b = 3, c, d, i = 20, j = 30, x;
x = a / b;
c = x++;
d = x / c;
d++;
i *= ++c;
j /= i++;
printf("x = %d c = %d d = %d i = %d j = %d", x, c, d, i, j);

return 0;
}

x = 4 c = 4 d = 2 i = 81 j = 0

4. The following code computes the prime factorization of a number a (≥ 2) given as input. For example,
if a = 72, it will print: Prime factorization = 2 x 2 x 2 x 3 x 3. Fill up the blanks
in this code. [5]
#include <stdio.h>
int main(){
int a, d = 2;
printf("Enter a positive integer: ");
scanf("%d", &a);
printf("Prime factorization = ");
do{
if (_____________){ // 1.5 marks
printf("%d x ", d);

______________; // 2 marks
}
else
______________; // 1.5 marks
}
while(a >= d);
printf("\b\b \b\b.\n");
return 0;
}

a%d==0 a = a/d d++

5. For the C program given below, what will be the output for the input 1234 5678 f (three data items
with two spaces in between) are given against a, b, c? [5]

6
#include <stdio.h>
int main() {
int a,b;
char c;
printf("Enter the values of a, b, c:");
scanf("%2d%3d%c",&a,&b,&c);
printf("a = %d\n b = %d\n c = %c\n",a,b,c);
scanf("%2d%3d%c%c",&a,&b,&c,&c);
printf("a = %d\n b = %d\n c = %c\n",a,b,c);
return 0;
}

Ans:
a = 12
b = 34
c=‘’
a = 56
b = 78
c=f

6. What is the output of the following program? [2]

#include <stdio.h>
int main() {
double x = 123456789.99;
int y = x;
printf("y = %d\n y = %0.2lf\n", y, (double)y);
return 0;
}

Ans:
y = 123456789
y = 123456789.00

You might also like