COL 100 Introduction to Programming- MINOR 1 IIT Jammu Time 1 Hr Max Marks 40 03.09.2016 NOTE: THERE 4 QUESTIONS IN ALL NOTE: 1. Do all the questions. 2. Write your name, entry number and group in all the sheets. 3. Use back side for rough work Q1 (10 marks) Q2 (7 marks) Q3 (8 marks) Q4 (10 marks) Q5 (5 marks) Total (40) 1. (10 marks) Do the following: a. Add 18 and -65 using a 8-bit 2's complement representation. (3 marks) Rough work ANSWER: 1 1 0 1 0 0 0 1 = -47 18 = 0 0 0 1 0 0 1 0 (mark 1) 65 = 0 1 0 0 0 0 0 1-65 = 1 0 1 1 1 1 1 1 (2's complement) (mark 1) 18 + (-65) = 1 1 0 1 0 0 0 1 (Negative number) = 0 0 1 0 1 1 1 1 (2's complement) (mark 1) = - 47 b. Convert the (234.25) 10 to its corresponding base 8 number. (3 marks) ANSWER: (234.25) 10 = (352.2) 8 (234) 10 = (352) 8 (mark 2) (.25) 10 = (.2) 8 (mark 1) c. Represent the fixed point real number (- 234.25) 10 using floating point representation in a 24-bit word machine, where 17 bits are used for mantissa to be represented in hexadecimal system and 7 bits are for exponent (including sign bits). State the maximum value of floating point real number stored in such system. (4 marks) ANSWER: 0 0 0 0 0 1 0 1 1 1 1 0 1 0 1 0 0 1 0 0 0 0 0 0 sign --------2---------------------- sign ------E------------- -------A------------ -----4-------------- -----0-------------- (- 234.25) 10 = (-EA.4) 16 = (-0.EA4 x 16 2 ) (mark 2) (mark 1) (mark 1) for representation 1
2. (7 Marks) Consider the following C program and write the output in the space provided. Show the working in rough space Rough work #include <stdio.h> main() { int i, j, x ; int c[10] = {1,2,3,4,5,6,7,8,9,0; int find (int x, int y) { return ( ( x < y )? 0 : x-y); void fun(int n) { int i, x=0; for (i=0; i < n; i++) { x+=i; //--------------------------------------------------------------------------------------- i=8; j=5; x=0; if ((5* (++i) +j) > 20) { x = x+ (--i)* j; Value of x = 40 { x = 7* (--j) % 6; (mark 1) Value of 5* ++i +j = 50; i = 8; j= 5; x+ (--i)* j = 40 Value of x= 40 //----------------------------------------------------------------------------------------- i=0; x=0; while (i < 5) { j= 0; while (j < i) { x += (i + j - 1); ++j; (mark 1) i++; Value of i, j = 1 1 Value of i, j = 2 1 Value of i, j = 2 2 Value of i, j = 3 1 Value of i, j = 3 2 Value of i, j = 3 3 Value of i, j = 4 1 Value of i, j = 4 2 Value of i, j = 4 3 Value of i, j = 4 4 Value of x = 30 Value of x = 30 2
//------------------------------------------------------------------------------------------- x=0; for (i=0; i < 10; ++i) if ((c[i] % 2) == 0) { x += c[i]; i+=2; (mark 2) Print the value of i, c[i] = 0 1 Print the value of i, c[i] = 1 2 Value of x = 2 Value of i = 3 Print the value of i, c[i] = 4 5 Print the value of i, c[i] = 5 6 Value of i = 7 Print the value of i, c[i] = 8 9 Print the value of i, c[i] = 9 0 Value of i = 11 //------------------------------------------------------------------ i=4; j= 5; x = find(i, find(i, j)); Value of x = 4 (mark 1) find(4, 5) = 0 x = find(i, find(i, j)) = find(4, 0) = 4; Value of x = 4 //---------------------------------------------------------------------------------------------- fun(9); Value of x = 36 (mark 2) Function 'fun' is adding integers from 1 to 8 Value of i and x = 0 0 Value of i and x = 1 1 Value of i and x = 2 3 Value of i and x = 3 6 Value of i and x = 4 10 Value of i and x = 5 15 Value of i and x = 6 21 Value of i and x = 7 28 Value of i and x = 8 36 Value of x = 36 //----------------------------------------------------------------------------------------------- 3
3. (8 marks) a. Write set of C statements to swap the values of two variables X and Y. You are not allowed to use any extra variable. (2 marks) x = x + y; y = x - y; (mark 2) x = x - y; b. Define a structure in C named 'Student' which contains four fields as { name, age, sex, department. Declare a variable as your name of 'struct Student'. Assign the values for your structure. (3 marks) struct Student (mark 2) { char name[15]; float age; char sex; char dept[25]; st1; struct Student st2 = {"Ayush", 17.5, 'm', "Computer Science"; (mark 1) c. Write an algorithm to compute your age as on today (3/Sept/2016) in terms of days, months, years. Input your age in day, month and year. Assume each month has 30 days for simplicity. (3 marks) input day, month, year; today_d =3; (mark 1/2) today_m =9; today_y =2016; if (day < today_d) then age_d = today_d - day { age_d = today_d+30 - day; (mark 1) today_m = today_m - 1 if (month < today_m) then age_m = today_m - month { age_m = today_m+12 - month; (mark 1) today_y = today_y - 1 age_y = today_y - year Output age_d, age_m, age_y; (mark 1/2) 4
4. (10 marks) a. Design an interactive command line calculator in C which will give options to the user for asking numbers and operations on it. Use switch-case to implement. Here is a sample output of the interface: $./a.out Please enter two numbers: 3 5 Please select the operation number you want to perform: 1 - add 2 - sub 3 - mul 4 - mod 1 Result is: 8 #include <stdio.h> main() { int a, b, x, result; (mark 4) scanf("input the values of two operands%d%d\n", &a, &b); scanf("input digit for operation\n%d", &x); switch (x) { case 1: result = a+b; break; case 2: result = a-b; break; (mark 5) case 3: result = a*b; break; case 4: result = a%b; break; printf("the result is: %d\n", result); (mark 1) 5. (5 marks) Write an algorithm which takes a positive integer as input, and prints which powers of 2 does the number lie between. For example, the number 269 lies between 28 (256) and 29 (512). If the input is 269, the output should be 8 9. Borderline cases which are powers of 2, such as 256, should be aligned to the lower limit of the desired range output, ie. 28 (256) and 29 (512). input num; quot = num; while (quot > 0) { quot = quot / 2; i = i+1; ; j = i-1; output( i-1, i) #include<stdio.h> main () { int num,quot,i=0 ; printf("input the value of num\n"); scanf("%d", &num); quot = num; while (quot > 0) { quot = quot / 2; i++; ; printf("%d %d", i-1, i); 5