Programming and Data Structure Tutorial sheet: 1 Topics: Data Handling, Operators and Expressions, Input and Output Operations. Q1. (a) Which one of the following is a valid name of a C variable: 2ab_c, Switch, xy#1, "rst"? ns: Switch. (b) What value does the following code print? #define N a*b int a = 5, b = 10, c = 15; printf("%d",c/n); ns: 30 (c) What will be the contents of the variables a and b after the execution of the following code on the input I do not know? char a, b; scanf("%c do not %c", &a, &b); ns: The characters I and k, respectively. (d) If the number of bits in the memory address of a computer is 16, what is the maximum number of addressable memory locations? ns: 2 16 = 65536. (e) What values does the following code print? int m = 7; int n = 9; m += n; n = m - n; m = m - n; printf ("%d, %d\n", m, n); ns: 9,7. (f) What values does the following code print? int m,n; m = n = 4; m *= 3/2; n = n * 3/2; printf("%d %d", m, n); ns: 4 6 (g)how many times is the statement i *= i+1; in the following for loop executed? for (i=1; i<100; ++i) i *= i+1; ns: Three times (h) How many times is the loop condition i<100 checked in the loop of Part (g)? ns: Four times (i) What is the value stored in the variable i immediately after the loop of Part (g) terminates? ns: 183 (j) What values does the following code print? int a = 7; int b = 4; int c = -2; a = a - a % b * c; printf ("%d\n", a); ns: 13 (k) Let the variables in the above code be defined as: int a = 5; int b = -3; int c = 0; Which of the following conditions evaluates to true in the C programming language? i. ( a < b ) ( c < b ) ii. ( c < a ) && ( c < b ) iii. ( a > b ) &&!c iv.!( a + b > c ) ns: iii
(l) Which of the following conditions is equivalent to the condition:!(( x >= y ) && ( y >= z ))? i.!( x >= z) ii. x <= z iii. ( x < y ) && ( y < z ) iv. ( x < y) ( y < z ) ns: iv (m) Fill in the blanks (for appropriate actions mentioned in brackets): # include <stdio.h> /* Include a header file for I/O operation. */ float x,y; int z; printf( Read x and y \n ); scanf( %f %f,&x,&y); /* Read x and y in appropriate format. */ z= (int) (x+y); /* Type-cast. (1) */ if (z = = 0) /* Check equality with zero. */ printf( x %f, y= %f \n,x, y); /* Print values of x, and y. */ (n) What values does the following code print? #define SNUM 10+10 SNUM * SNUM; printf ( "pnum = %d\n", pnum ); ns: 120 (o) What values does the following code print? int k; int num = 30; k = num > 5? (num <= 10? 100:200):500; printf ( "k = %d\n", k ); ns: 200 (p) What does the following program print? #include <stdio.h> main () int i = 100, m, n; m = 1 + (i++); n = 1 + (++i); printf("m = %d, n = %d\n",m,n); ns : m = 101, n = 103 (q) The RM size of a computer is 64Mbytes. How many char type data can it store? ns: 1 Character occupies 1 byte. Number of character data that can be stored = 64 Mbytes/1byte 64X2 20 or, 64*1024*1024 or, 68,719,476,736 Q 2. For questions 1.1 to 1.5 assume that variables a and b have data type int and variable c and d have data type float. lso, a =9, b = 8, c = 16.0, and d = 6.0. For each question write the value assigned to the variable z. Data type of z is float. 1.1. z = a + c / 4 * d / 3 + b; ns : 25 1.2. z = c + a / 4 * b / 3 + d ; ns :27 1.3. z = (int) c / a * b / 3 ; ns :2 1.4. z = a / b * b % 5 % 3 ; ns :16 1.5. z = ( a >= c )? a : c ; ns :0
Q3: (a) Consider the following snippet of a c-program for which int and unsigned data-types are represented by 8-bits each. Write the minimum and maximum possible values which can be correctly stored in the variables a and b. Show details of your steps. int a; unsigned b;.... ns: int a: i) largest 2 7-1 ii) minimum: - 2 7 unsigned a: iii) largest 2 8-1 iv) minimum: 0 (b) Write a program which will take your iit kgp roll no as input and print your roll no, department and year of admission as output. You must supply your complete IITKGP roll number to the program (like 09FB1331). #include <stdio.h> char roll[10]; printf("enter your roll number : "); scanf("%s",roll); printf("roll number : %s\n", roll); printf("department : %c%c\n", roll[2], roll[3]); printf("year : %d\n", 2010 + (int)(roll[1]- 0 )); The last three lines in the output of the above program are: Roll number: 11CS1331 Department: CS Year: 2011. Q4: Convert the following decimal numbers into 2 s complement 8-bit integer representations. -37, 79. Soln: (a) -37: 37->37/2 =18 ->1 18/2=9 ->0 9/2=4 ->1 4/2=2 ->0 2/2=1 ->0 1 / 2 =0 ->1 Hence: 37 -> 00100101 (in 8 bit) -37= 2 s complement of 37= 1 s complement of 37 + 1= 11011010 + 1 11011011 (ns.) (b) 79 ->79/2=39 ->1 39/2=19 ->1 19/2= 9 ->1 9/2=4 ->1 4/2= 2 ->0 2/2=1 ->0 1 / 2=0 ->1 Hence: 79->01001111 (ns.) Q5:. Consider the following C-code which intends to compute simple interest on an amount of Rs. 2000/- for a period of four years at simple interest rate of 10.5% per annum. # include <stdio.h> int amount = 2000; int time = 4 ; float rate = 10.5;
float simint; simint = time/100 * amount * rate; printf( Interest on Rs %d at %f per annum for %d y = %d\n, amount, rate, time, simint); return 0; What will be the output given generated by the above code? Write the reason(s) in one sentence. ns: Interest on Rs. 2000 at 10.5 per annum for 4 y = 0 Reason: Integer division of time/100 results in 0. Modify the above code so that you get the correct output. Explain your answer in one sentence. ns: simint = amount * rate * time/100; or simint = ((float)time/100) * amount * rate; or simint = (time/100.0) * amount * rate; Reason: In either of the solutions floating point division occurs. Q6: What will be the output generated by the following code? char x = ''; static char name[20] = "NIL KUMR GUPT"; printf("output OF CHRCTERS\n\n"); printf("%c\n%3c\n%5c\n", x,x,x); printf("%3c\n%c\n", x,x); printf("\n"); printf("output OF STRINGS\n\n"); printf("%s\n", name); printf("%20s\n", name); printf("%20.10s\n", name); printf("%.5s\n", name); printf("%-20.10s\n", name); printf("%5s\n", name); ns : Output OUTPUT OF CHRCTERS OUTPUT OF STRINGS NIL KUMR GUPT NIL KUMR GUPT NIL KUMR NIL NIL KUMR NIL KUMR GUPT
Q7: Write a program that determines whether a given integer is odd or even and displays the no. and description on the same line. Soln: #include<stdio.h> int num; printf ("Enter a number to be checked for even/odd: "); scanf ("%d",&num); if (num%2==0) printf ("The entered number is EVEN.\n"); else printf ("The entered number is ODD.\n"); Q8: In this exercise, we compute the binomial coefficient n C r by repeatedly using the formula n C r = ( n/r) n-1 C r-1. We compute n/r as a floating-point value. Finally, the accumulated product is rounded to the nearest integer. In both the following parts, you are not allowed to use any math library function. Fill in the blanks to complete the following C function that takes a floating-point value x as its only argument and returns the rounded value of x. The rounded value of x is the integer nearest to x. When x is mid-way between two consecutive integers, we follow the convention round half away from zero, that is, round (2.5) = 3 and round ( 2.5) = 3. int roundit ( double x ) int r; /* The rounded integer to return */ double fpart; /* Fractional part */ /* Store in r the truncated value of x */ r = (x >= 0)? (int)x : (int)(-x) ; /* Store in fpart the fractional part of x */ fpart = (x >= 0)? x - (double)r : -(x + (double)r); /* Modify r based conditionally upon fpart */ if (fpart >= 0.5) ++r ; /* Return r after sign adjustment */ return (x >= 0)? r: -r ;