0% found this document useful (0 votes)
6 views

18_spring_soln

The document contains solutions to a mid-semester examination for a programming and data structure course, covering topics such as binary number representation, IEEE 754 floating point format, C programming outputs, and algorithms for finding perfect numbers. Each question includes detailed answers, calculations, and code snippets. The document serves as a comprehensive guide for students to understand key concepts and programming techniques.

Uploaded by

Arya Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

18_spring_soln

The document contains solutions to a mid-semester examination for a programming and data structure course, covering topics such as binary number representation, IEEE 754 floating point format, C programming outputs, and algorithms for finding perfect numbers. Each question includes detailed answers, calculations, and code snippets. The document serves as a comprehensive guide for students to understand key concepts and programming techniques.

Uploaded by

Arya Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MID-SEMESTER EXAMINATION

CS10001: Programming and Data Structure (Solutions)

Question 1: [2.5+2.5+2=7]

(a) Interpret 11011011 as a two's complement binary number, and


give its decimal equivalent.
Answer:

First, note that the number is negative, since it starts with a 1.

Change the sign to get the magnitude of the number.

1 1 0 1 1 0 1 1
¬ 0 0 1 0 0 1 0 0
+ 1
0 0 1 0 0 1 0 1
Convert the magnitude to decimal: 001001012 = 2516 = 2×16 + 5 = 3710.

Since the original number was negative, the final result is ­37.

(b) Show how a computer would perform 10 + ­3 using eight bit two’s
complement representation. Is there a carry? Is there an overflow?
Answer:

10 + ­3 = 7:
1 1 1 1 1
0 0 0 0 1 0 1 0
+ 1 1 1 1 1 1 0 1
0 0 0 0 0 1 1 1
Carryout without overflow. Sum is correct.

(c) Convert the 8­bit signed two’s complement hex number 0x3F to
decimal.

Answer: Easiest is to convert to binary: 0011 1111. Then convert to


decimal: 32 + 16 + 8 + 4 + 2 + 1 = 63, which is the answer

Question 2: [4+4=8]

(a) Convert ­35.75 to IEEE 754 floating point format. What is the hex
representation of the resultant bit pattern?

Answer: First, we need to write down the binary representation of


35.75: 100011.11
Next, we need to put normalize it: 1.0001111 * 25
Now we can get the 3 pieces of the number we need:
S: 1 (negative)
E: 132 (127 + 5) = 1000 0100
M: 000 1111 0000 0000 0000 0000
Now we just put all the bits together: 1100 0010 0000 1111 0000 0000
0000 0000 and convert each group of 4 to a hex digit:
Answer: 0xC20F0000
(b) Convert the IEEE format floating point number 0x40200000 to decimal.

Answer: First, convert the hex to binary: 0100 0000 0010 0000 0000
0000 0000 0000
Then pull out each of the 3 pieces:
S: 0 (positive)
E: 1000 0000 = 128. Taking 128 ­ 127 = 1
M: 010 0000 0000 0000 0000 0000 So we have 1.01 * 21 = 10.1
Then convert that to decimal to get the answer: 2.5

Question 3:

What is the output of the following C Programs? [3+3+3 = 9]

(a) #include <stdio.h>

int main()

int a = 10, b = 20;

int *p = &a, *q = &b;

printf("%d ", (*p)++);

printf("%d ", ­­(*q));

printf("%d\n", a+b);

return 0;

Answer: 10 19 30

(b) #include<stdio.h>

int *fun(int p, int *q)

p = 4;

*q = 2;

return (q);

int main()

int x = 6, y = 9, z = 3;

z = *fun(x, &y);
printf("%d %d %d\n", x, y, z);

return 0;

Answer: 6 2 2

(c) #include<stdio.h>

int main()

float arr[5] = {1.5, 2.5, 3.5, 4.5, 5.5};

float *ptr1 = &arr[1];

float *ptr2 = ptr1 + 3;

float *ptr3 = ptr2 + 1;

printf("%f ", *ptr2);

printf("%ld ", ptr2 ­ ptr1);

printf("%f\n", arr[ptr3 ­ ptr1]);

return 0;

Answer: 5.500000 3 5.500000

Question 4:

A lazy programmer wants to write a program that will perform two


activities together ­­ (a) reverse the elements of an integer array, and
(b) sum the elements of the array. (S)he thought of writing an
additional function to exchange the values of two variables. However,
(s)he feels that it can be done without introducing any temporary
variable within that function, exchange(int *p, int *q), which takes two
pointers p and q containing the addresses/locations of two integer
elements of an array and then it exchanges the values in those two
locations. With this thought, (s)he started writing the program, but did
not complete it. Help the lazy programmer to complete the program
correctly. The part of the code is provided below. [1.5 x 4 = 6]

#include<stdio.h>

void exchange(int *p, int *q)

*p = *p + *q;

*q = *p ­ *q ;
*p = *p ­ *q ;

int main()

// defining an array

int a[] = {7,4,8,2,9,0,1,3,6,5};

// marking the initial index of the array

int i=0;

// marking the final index of the array

int f = sizeof(a)/sizeof(int) ­ 1;

// initializing the sum

int sum = 0;

while ( i < f ) // converge from both ends of the array index

// updating the sum (adding two elements of the array at a time)

sum = sum + a[i] + a[f] ;

exchange( &a[i] , &a[f] ); // call to the exchange function

i = i + 1 ; // incrementing initial index

f = f ­ 1 ; // decrementing final index

return 0;

Question 5:

The following program finds an approximate value of a definite integral.


Let l be the left and r the right boundary for the integral. Also let h
be the step size. The idea is to break the interval [l, r] into sub­
intervals [l, l + h], [l + h, l + 2h], [l + 2h, l + 3h], . . . , [r − h,
r]. Assume that r − l is an integral multiple of h. The program
evaluates the function to be integrated at the center of each interval,
multiplies these values by the width h and computes the sum of these
products as the approximate integral. Suppose you are integrating the
function x2. Complete the following program. Each blank can have at most
one statement. [ 1 + 2 = 3 ]

#include <stdio.h>

int main ()
{

double l, r, h, x, s;

printf("Enter left boundary : "); scanf("%lf", &l);

printf("Enter right boundary : "); scanf("%lf", &r);

printf("Enter step size : "); scanf("%lf", &h);

s = 0;

for (x = l; x < r ; x += h){

s += h*(x + h/2)*(x + h/2) ;

printf("Integral = %lf\n", s);

return 0;

Question 6:

The following program attempts to find the roots of the polynomial x 3 −


4x2 − 4x + 16. It uses the fact that the product of the roots is the
negative of the constant term of the polynomial. For instance, x 3 − 4x2 −
4x + 16 has roots, 2, −2, 4, and the product of the roots: 2 × −2 × 4 =
−16 (negative of the constant term 16). To find the roots, the program
checks for each integer r if it is a factor of the constant term 16, and
if so, whether replacing x by the integer r in the polynomial evaluates
to zero. For instance, since 4 is a root, 43 − 4 × 42 − 4 × 4 + 16 = 0.

Complete the following program. Each blank can have at most one
statement. [1 + 2 + 1 = 4]

#include <stdio.h>

int main ()

int r, nroot = 0;

for (r=1; nroot<3; r++) {

if (16 % r == 0) {

if (r*r*r­4r*r­4*r+16==0) {

printf("Root found : %d\n",r);

nroot++ ;

if ( ­r*r*r ­ 4r*r + 4*r + 16 == 0 ) {


printf("Root found : %d\n",­r);

nroot++ ;

return 0;

Question 7:

The following program randomly generates a sequence of integers between


­8 and +91 and outputs the maximum and minimum values generated so far.
It exits if a negative integer is generated. The program does not store
the sequence generated (say, in an array), but updates the maximum and
minimum values on the fly, as soon as a new entry is generated. Complete
the following program. Each blank can have at most one statement.[1×4=4]

Hint: To generate a random number between 0 to n, you can use

rand()%(n+1)

So, for example, you can get a random number between 5 to 90 by ­­


rand()%86 + 5

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main ()

int a, i, max, min;

i = 1;

while (1) {

a = rand()%100 – 8 ;

//a stores a random number between ­8 to +91

printf("Iteration %d: new entry = %d, ", i, a);

if ( a < 0 ) {

printf("...exiting...\n\n\n");

break;

}
if (i == 1) max = min = a ;

else {

if (a > max) max = a;

if (a < min) min = a ;

printf("max = %d, min = %d\n", max, min);

++i;

return 0;

Question 8:

What will be the outputs of the following programs? [2 + 2 = 4]

(a) #include<stdio.h>

int main()

int x = 0, y = 10, z = 20;

while (1) {

x++;

if (y > z) break;

y += 4*x; z += 2*x;

printf("x = %d, y = %d, z = %d", x, y, z);

return 0;

Answer: x = 4, y = 34, z = 32

(b) #include<stdio.h>

int main()

int s = 0;

while (s++ < 10) {


if ((s < 4) && (s < 9)) continue;

printf("%d ", s);

return 0;

Answer: 4 5 6 7 8 9 10

Question 9:

What will be the output of the following C programs? Only one of the 4
choices are correct. Circle only the correct choice. [5x2 = 10]

(a) int fun(int *a,int n)

n++;

*a = *a + n;

return *a;

int main()

int n=10;

n = fun(&n,++n);

printf("%d",n);

return 0;

A. 20 B. 21 C. 22 D. 23

(b) #include <stdio.h>

int n = 17, r=35;

void my_disp_func( int r ) { printf("%d,%d\n",n,r); }

int main ()

int n = 21, r=14;


my_disp_func(n);

A. 17,21 B. 21,14 C. 17,35 D. 21,35

(c) void h( int A[], int n )

int i;

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

A[i] *= A[i­1];

int main ()

int A[5] = {1,2,3,4,48};

h(A,4);

printf("%d", A[4]/A[3]);

return 0;

A. 16 B. 48 C. 2 D. 4

(d) #include <stdio.h>

int f(int i)

return i%2;

int main()

int i=27;

while(f(i))

printf("%d", i);

i = i/2;

}
return 0;

A. 27 B. 27 13 C. 2713 D. 1

(e) #include <stdio.h>

void jumble(char A[], int size)

int i;

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

A[i] = A[size ­ i ­1];

int main()

char A[] = "REMARKABLE";

jumble(A, 10);

printf("%s", A);

return 0;

A. REMARKABLE B. ELBAKRAMER C. ELBAKKABLE D. None of the above

Question 10:

A natural number N is called a 'perfect number' if the divisors of N


that are less than N add to N. Example: 6 is a perfect number because
the divisors of 6 that are less than 6 (, namely 1,2, and 3) add to 6.
Given below is a program that prints all perfect numbers in a given
range using functions. Fill in the blanks. [1 x 5 = 5]

/**

* C program to print all perfect numbers in given range using function

*/

#include <stdio.h>

/* Function declarations */

int isPerfect(int num);


void printPerfect(int start, int end);

int main()

int start, end;

/* Input lower and upper limit to print perfect numbers */

printf("Enter lower limit to print perfect numbers: ");

scanf("%d", &start);

printf("Enter upper limit to print perfect numbers: ");

scanf("%d", &end);

printf("All perfect numbers between %d to %d are: \n", start, end);

printPerfect(start, end);

return 0;

/**

* Check whether the given number is perfect or not.

* Returns 1 if the number is perfect and 0 otherwise.

*/

int isPerfect(int num)

int i, sum;

/* Finds sum of all proper divisors */

sum = 0;

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

if( num%i == 0 )

{
sum = sum + i ;

if( sum == num )

return 1;

else

return 0;

/**

* Print all perfect numbers between given range start and end.

*/

void printPerfect(int start, int end)

/* Iterates from start to end */

while(start <= end)

if( isPerfect(start) )

printf("%d, ", start);

start++ ;

You might also like