Math 211 Test 1, February 19, 1997 Instructions. Please enter your AFS-id and ID number. Problems 1 to 8 worth 10 points each. If you are asked for output, give your output in EXACTLY the same way the computer will generate; the output should be three lines or less, so you should have enough space for each problem. If you are asked to point out the errors of a program, (a) list the line number, and then (b) correct the errors by writing the correct version. Problem 9 worth a total of 20 points. Each sub-problem worth 4 points. Write your answer for each sub-problem. The exam is given under the provision of the Honor Code. Only the first page is to be handed in. Yellow Version Problem 1: Instructions: Give the output for the following program: #include main() { int i = 23, x = 0; printf("Just for Practice.\n"); while (i > 1) { if (i%5 == 0) { x += i; printf("%d, ", x); } --i; } printf("x = %d\n", x); return 0; } Problem 2: Instructions: Give the output for the following program: #include main() { int digit = 7; while(digit >= 3) printf("%d", --digit); printf("\n"); return 0; } Problem 4: Instructions: Give the output for the following program: #include #include main() { char ch; for ( ch = 'g'; ch >= 'a'; ch--){ putchar(ch); putchar(toupper(ch)); printf(", "); } printf("\n"); return 0; } Problem 5: Instructions: Give the output for the following program: #include main() { int i, flag, x = 0; for (i = 1; i < 7; ++i) { flag = i % 3; switch (flag) { case 0: x += 3; break; case 1: x++; break; default: x += 10; break; } printf("%d, " , x); } printf("x = %d\n" , x) ; return 0; } Problem 3: Instructions: Give the output for the following program: #include main() { int j = 7, k = 12; float a; a = k/j; printf("a = %.2f, j = %d\n", a, j++); a = (float) k / j; printf("a = %.2f, j = %d\n", a, ++j); j--; a = (float) k / (float) j; printf("a = %.2f, j = %d\n", a, j); return 0; } Problem 6: Instructions: Give the output for the following program when the user gives 5 as input. #include #define CONST 5 main() { int sum = 0, n, k; printf( "Enter a decimal integer: " ); scanf( "%d", &n ); for ( k = 1; k < 4; k++){ sum += n + CONST; } printf( "n = %d, sum = %d.\n", n, sum ); if ( sum < 20 ) printf( "The sum is small.\n" ); else printf( "Oops, too big.\n" ); return 0; } Problem 7: Instructions: The following program is supposed to print the tenth digit of a two digit number. However, there are 2 errors in the program. Although it compiles, the result is not as expected. Give the line number(s) where the errors occur, and give the corrected code for that line. line number 1. #include 2. main() 3. { 4. int n, m; 5. /* initialize and read in a value for n */ 6. printf("Enter a two-digits interger: "); 7. scanf("%d", n) ; 9. /* Get the tenth digit */ 10. m = n / 10; 11. printf("The tenth digit is: %d\n", &m); 12. return 0; 13. } Problem 8. Instruction: The following program is supposed to convert a letter grade to "Passing-Failing system". However, there are 5 errors in the program so that it will not even compile. The compilier gives the following error message: undefined symbol: A integral constant expression expected undefined symbol: B integral constant expression expected duplicate case in switch: 1 undefined symbol: C integral constant expression expected duplicate case in switch: 1 undefined symbol: D integral constant expression expected duplicate case in switch: 1 undefined symbol: F integral constant expression expected duplicate case in switch: 1 Give the line number(s) where the errors occur, and give the corrected code for that line. line number 1. #include 2. #include 3. main() 4. { 5. char ch; 6. printf("Please enter a grade (A, B, C, D, F): ") ; 7. ch = getchar(); 8. ch = toupper( ch ); 9. switch( ch ) { 10. case A: case B: case C: case D: 11. printf("Good. You passed the course.\n"); break; 12. case F: 13. printf("Sorry. You failed the course.\n"); break; 14. default: 15. printf("Illegal letter grade.\n"); break; 16. } 17. return 0; 18. } Problem 9: Give the answer for the variables after the assignment below: (a) int i, j, k; i = 4; j = 2; k = 3; k++; i--; j *= k + i; Answer j = (b) char c1, c2, c3; c1 = 'a'; c2 = 'A'; c3 = 'D' + c1 - c2 + 1; Answer c3 = (Your answer should be a character in ' ', remember the answer is case sensitive). (c) int i, j, k; i = 3; j = 5; k=7; k = i <= 2 ? j : i; Answer k = (d) float c = 10.0, interest = 0.01; float credit = 2.4, debt = 3.4; c *= interest + 1.0; c += credit - debt; Answer c = (e) int i = 0, j = 1, k = 3, n = 3; n = i <= j && k < n; Answer n =