C Programming
Q141.
Consider the C program fragment below which is meant to divide x by y using repeated subtractions. The variables x, y, q and r are all unsigned int. while (r >= y) { r = r - y; q = q +1; } Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x==(y*q+r)?Q142.
Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n=d_{1}d_{2}...d_{m}. int n, rev; rev = 0; while (n > 0) { rev = rev*10 + n%10; n = n/10; } The loop invariant condition at the end of the i^{th} iteration is:Q143.
Consider the following pseudo code, where x and y are positive integers. begin q := 0 r := x while r \geq y do begin r := r - y q := q + 1 end end The post condition that needs to be satisfied after the program terminates isQ144.
Consider the following C program main() { int x, y, m, n; scanf ("%d %d", &x, &y); /* Assume x > 0 and y > 0 */ m = x; n = y; while (m! = n) { if (m > n) m = m - n; else n = n - m; } print f ("% d", n); } The program computesQ145.
The following function computes X^{Y} for positive integers X and Y. int exp(int X,int Y){ int res=1, a=X, b=Y; while (b!=0){ if(b%2==0){a=a*a; b=b/2;} else {res=res*a; b=b-1;} } return res; } Which one of the following conditions is TRUE before every iteration of the loop?Q146.
The following function computes the maximum value contained in an integer array p[] of size n (n>=1). int max(int*p, int n){ int a=0,b=n-1; while (__________){ if (p[a]<=p[b]){a=a+1;} else {b=b-1;} } return p[a]; } The missing loop condition isQ147.
Consider line number 3 of the following C-program. int min ( ) { /* Line 1 */ int I, N; /* Line 2 */ fro (I =0, I < N, I++); /* Line 3 */ } Identify the compiler's response about this line while creating the object-module:Q148.
Consider the following C code. Assume that unsigned long int type length is 64 bits. unsigned long int fun(unsigned long int n){ unsigned long int i, j = 0, sum = 0; for (i = n; i > 1; i = i/2) j++; for ( ; j > 1; j = j/2) sum++; return(sum); } The value returned when we call fun with the input 2^{40} isQ149.
Consider the following C-function in which a[n] and b[m] are two sorted integer arrays and c[n+m] be another integer array. void xyz (int a[],int b[],int c[]){ int i, j, k; i=j=k=0; while((i < n))&&(j < m) if (a[i] < b[j]c[k++]=a[i++]; else c[k++]=b[j++]; } Which of the following condition(s) hold(s) after the termination of the while loop ? I. j\ltm, k=n+j-1, and a [n-1]\ltb[j] if i=n II. i\ltn, k=m+i-1, and b[m-1]\leqa[i] if j=mQ150.
What does the following algorithm approximate? (Assume m \gt 1, e \gt 0). x = m; y = 1; while (x - y > e) { x = (x + y)/2; y = m/x; } print(x);