CSE 130 Fall 2009 Solutions to Ex-1 1. (a) N (b) Y (c) Y (d) Y (c) N 2. (a) 1 (b) 4, % is done first (c) False (d) False (a, b) are worth 2 points, and (c, d) are 3 each. 3. (a) Y (b) When C1 is true and C2 is false. (c) if (C1 && C2) { S1; } else if (C1 && !C2) { S2; } // No else for the second if. 4. (a) Code-I: 5 Code-II: Nothing (b) None of these (c) For Code-I, i increases and n decreases after every iteration. Initially i <= j, so loop starts. But eventually n becomes negative, making j < i. This terminates the loop. For Code-II, n < i is always false (i is n-1) for any value of n supplied by the user. So this loop will not be done even once. So it is terminated by default. 5. (a) R = a; // What is left in a is remainder. (b) int a, b Q, R; for (Q = 0; a >= b; Q++) { a = a - b; } R = a; 6. (a) Either a comma missing between array and a[3], or one of two variables should not be there. Compiler will treat array as a variable name. (b) !== is not correct. This is an operator error. Correct operator should be != or just ==. 7. int main(void) { float x; int i; // Can be on the same line. scanf("%f", &x); // (2+4+2) points i = (int)(x + 0.5); // There are other answers. printf("%d", i); return 0; } 8. (a) The example was given for an array of size 4. Here size is 10. // Initialize array A for (i = 0; i < 10; i++ ) // (1+3 points) A[i] = (float)(i * i * i)/m; // Copy A to B in reverse order. for (i = 0; i < 10; i++ ) // (1+3 points) B[9 - i] = A[i]; // There are several answers. 9. int flip (int n) // Header, 1.5 points { int i, j, m; // Local variables. 1 point i = n/10; // Extract left digit. 1 point j = n%10; // Extract right digit 1 point m = j*10 + i; // Flip digits 1.5 points return m; // 1 point } 10. The part of the original program is included. int main (void) { int m, n, count = 0, i, j; // Use m, n in your program. // Complete nested while loops. Assume m and n have // been initialized. i = 1; while ( i <= n ) { j = 1; // 0.5 point while (j <= n) // 1 point { if (i+j == m) count++; // 2 points j = j+1; } // 0.5 point i = i+1; // 0.5 point } // Close outer loop // 0.5 point // No need to write a statement for printing count. return 0; }