indian institute of technology kharagpur
play

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Date: 18/02/2016 FN - PDF document

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Date: 18/02/2016 FN Time: 2 hours Full marks: 60 No. of students: 680 (approx.) Spring Mid Semester Exams, 2016 Dept: Computer Science and Engineering Sub No: CS11001 B.Tech. 1 st Year Sub


  1. INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Date: 18/02/2016 FN Time: 2 hours Full marks: 60 No. of students: 680 (approx.) Spring Mid Semester Exams, 2016 Dept: Computer Science and Engineering Sub No: CS11001 B.Tech. 1 st Year Sub Name: Programming and Data Structures Instructions : Answer all questions in the spaces (box or fill-in-the-blank) provided. 1. What do the following programs print? [4×5 = 20 marks] a) #include <stdio.h> void print(int n) { if (n > 4000) return; printf ("%d ", n); print (2*n); printf ("%d ", n); 1000 2000 4000 4000 2000 1000 } int main() { print(1000); return 0; } b) #include <stdio.h> void what (int n, int a[]) { int i=3, j=2; 10 7 30 3 50 a[i] = i++; a[--j] = --i + 4; } int main() { int i; int x[] = {10,20,30,40,50}; what (5,x); for (i=0; i<5; i++) printf ("%d ", x[i]); } c) #include <stdio.h> int k=50; int f (int k) { 25 26 1250 k=k+1; return k; } int g (int x) { return (x*k); }

  2. int main() { int a, k=25; a = f(k); printf ("\n%d %d %d", k, a, g(k)); } d) #include <stdio.h> 26 32 38 44 50 56 int x=20; 11 11 11 11 11 11 void f (int b, int x) a=12, x=20 { static int a=5; if (b==0) { printf ("\n"); return; } a++; x+=6; printf ("%d ", x); f(b-2,x); printf ("%d ", a); } int main() { int a=12; f(a,x); printf ("\na=%d, x=%d", a,x); } e) #include <stdio.h> int mystery (int a[]) { 30 70 90 return (a[0] + a[1]); } int main() { int x[] = {10,20,30,40,50,60}; printf ("\n%d %d %d", mystery(x), mystery(&x[2]), mystery(x+3)); } 2. The following is an incomplete recursive function for printing the digits of an integer, n , from left to right. Assume that n is greater than or equal to 0. The following printf statement must be inserted at an appropriate position inside the function: printf ("%d", n%10); In order to complete the function, you have to fill in the blank appropriately, and insert the above statement in an appropriate position inside the function. [4 marks] void fun2(int n) { if (n == 0) return; fun2 ( n/10 ); <insert here> }

  3. 3. For an integer k we have a 2k = (a 2 ) k and a 2k+1 = (a 2 ) k * a Use this observation to fill in the blanks to complete the following recursive function for computing x n . float expsqr( float x, int n ) { if (n < 0) return expsqr( 1/x, -n ); else if (n == 0) return 1 ; else if ( n == 1 ) return x; else if ((n%2) == 0) return expsqr( x*x, n/2 ); else return ( x * expsqr( x*x, (n-1)/2 ) ); } [5 marks] 4. What should be the input to the following program such that it prints the word “Success”? #include <string.h> void main( ) { Bellow char *stra = "Hello"; char strb[20]; scanf("%s", strb); if (strlen(strb)==6) { strb[6] =  J  ; if ((strb[0] ==  B  )&&(strb[5] ==  w  )) { strb[5] =  \0  ; strb[0] =  H  ; if (!strcmp(stra, strb)) printf("Success"); } } [5 marks] }

  4. 5. Consider the following code. The function myStrcat concatenates two strings. It appends all characters of b to end of a. So the expected output is “Quo Vadis”. The program compiles fine but produces segmentation fault when run. #include <stdio.h> void myStrcat (char *a, char *b) { int m = strlen(a); int n = strlen(b); int i; for (i = 0; i <= n; i++) a[m+i] = b[i]; } int main() { char *str1 = "Quo "; char *str2 = "Vadis"; myStrcat(str1, str2); printf("%s ", str1); return 0; } Which of the following changes may be necessary for the program to print “Quo Vadis”? A combination of these may be necessary. (A) char *str1 = “Quo “; can be changed to char str1[100] = “Quo “; Only (A). (B) char *str2 = “Vadis“; can be changed to char str2[100] = “Vadis“; (C) A line a[m+n  1] =  \0  is added at the end of myStrcat [3 marks] 6. The function int PC (int k) returns the number of distinct prime factors of its argument k. For example, if k=156, the function will return 3 (since there are 3 distinct prime factors 2, 3 and 13). Fill in the blanks with appropriate C expressions. [5 marks] int PC (int k) { int num, ____count=0____; for (___num=2____; _num<=k_or k!=1___; ++num) { if (k%num == 0) { ++count; while (____k%num == 0___) __k /= num____; } } return count; }

  5. 7. The following function computes and returns the distance between two points (x1,y1) and (x2,y2) on a two-dimensional plane. Fill in the blanks appropriately. [3 marks] #define SQR(VAL) __((VAL)*(VAL))___ float dist (float x1, float y1, float x2, float y2) { return sqrt(SQR(____x1-x2_______) + SQR(____y1-y2___)); } 8. The function search finds out whether a given integer “key” is present in an array “list”. The array “list” contains only positive elements, with the delimiter -1 used as the last element. The function returns the index of the array if a match is found, and returns -1 otherwise. Fill in the blanks appropriately. [4 marks] int search (int key, int list[]) { int index=0; while (___list[index]!=-1_____) { if (____key==list[index]____) return index; index++; } __return -1____; } 9. Suppose arr and brr are integers arrays of length more than n . Consider the execution of the following when i and j are integer variables. for (i=0;i<n;i++) { brr[i]=0; for (j=0;j<i;j++) if (arr[i]>arr[j]) brr[i]++; }; (a) If all the elements of the the array, brr[0] through brr[n-1], are zero after the execution of the above statement, then which (one or more) of the four statements could apply to the elements of the array arr from arr[0] through arr[n-1]. [3 marks] (i) strictly increasing (ii) strictly decreasing (ii), (iii) and (iv) (iii) non-increasing (iv) non-decreasing . (b) Further, if we use an array crr of n integers and do the following: for (i=0;i<n;i++) { crr[brr[i]]=arr[i]; } for (i=0;i<n;i++) { arr[i]=crr[i]; } Then, assuming all the n elements in array arr are distinct, how does the execution of the above two statements alter array arr ? [3 marks] Sorts array arr in increasing order

  6. 10. What does the program below print for input k, when k=2, 231, 3367 and 88? (The C function log10 computes the logarithm to the base 10. The functions sin and tan are the sine and tangent trigonometric functions, respectively. The atan and asin functions are the corresponding inverse trigonometric functions.) [2+3=5 marks] #include <stdio.h> #include <math.h> int i,k; double x,y,z; 1, 2.000000 3, 2.000000 int main() { 4, 2.000000 scanf ("%d", &k); x=k; y=log10(x); 2, 2.000000 z = tan(atan(1)) + x*asin(sin(1/x)); while (i<=y) i++; printf ("%d, %f;\n", i,z); }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend