UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS106 MIDTERM - Feb 24, 2005 EXAMINERS: M. Bussmann, D.C.S.
Kuhn, D. Potyondy, L. Shu
circle the name of your instructor
Student Name (print): Student Signature: Student Number:
DO ALL WORK IN THIS BOOKLET. DO NOT REMOVE THE STAPLE.
Important: Marks will be awarded for correctness of your algorithm, C syntax, adherence to recommended C coding practice, code efficiency, and the clarity of your program. This is an open book exam (Type X). All calculators allowed; no computers allowed. Time allotted: 1 hour
Question 1 2 3 4 Total
Maximum Mark 5 5 5 10 25
Actual Mark
Page 1 of 6
Question 1.
a) (2 marks) What is the output of the following code?
#include <stdio.h> int main() { int result, a = 10, b = 3, c = -2, d = 13 ; result = b + d / - c % a + c ; printf("Result: %d\n", result ); if ( result++ < 3 ) printf("Case else if ( result < 8 ) printf("Case else if ( ++result <= 9 ) printf("Case else if ( result++ <= 11 ) printf("Case else printf("Case printf("Result: %d\n", result ); return 0; } A\n"); B\n"); C\n"); D\n"); E\n");
b) (3 marks) What is the output of the following code?
#include <stdio.h> int main() { int i, j, prod; for ( prod = 1, i = 1; i < 8; i += 2 ) { if ( !(i % 3) ) continue; prod *= i; j = i; while ( j++ < 3 ) printf("Hello\n"); } printf("prod = %d, i = %d\n", prod, i); return 0; }
Page 2 of 6
Question 2. (5 marks)
What is the output of the following program, given the following input file? myfile.txt
#include <stdio.h> float myfunction(int n, float f); FILE *myfile; int main(void) { int n; float x, f = 1.0; if ( (myfile=fopen("myfile.txt","r")) == NULL) { printf("fopen error\n"); return 1; } fscanf(myfile, "%d", &n); x = myfunction(n, f); printf("f = %.1f, x = %.1f \n", f, x); fclose(myfile); return 0; } float myfunction(int n, float f) { int i; float num, y = 0.0; for (i=0; i<n; i++) { fscanf(myfile, "%f", &num); y += num*f; f = f+1.0; } printf("num = %.1f, f = %.1f\n", num, f); return y/n; } 5 2.0 1.0 4.0 3.0 5.0
Page 3 of 6
Question 3. (5 marks)
The purpose of the following C code is to count the number of occurrences of the digit 2 in a list of positive integers input from the keyboard. The user inputs a negative integer to end input. The code contains at least 5 errors: syntax errors (errors that the compiler will notice), logic errors (errors that result in an incorrect output, but have correct syntax), and/or runtime errors (errors that cause the program to fail during execution). In the table below state the error and error type. Note: The numbers on the left indicate line numbers for reference and are not part of the code.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. #include <stdio.h> int main(void) { int number, digit, count=0; printf("Input positive integer (neg to end) "); scanf("%d",number); while (number >= 0) { while (number >= 0) { digit = number / 10; if (digit = 2) count++; number = number / 10; } printf("Input positive integer (neg to end) "); scanf("%d",&number); printf("Number of 2s in the list is: %d\n", count); return 0; }
1 2 3 4 5
Page 4 of 6
Question 4. (10 marks)
Write a function that corresponds to the following declaration/prototype:
int transform (int num);
You may assume that the function has access to stdio.h and math.h. The function should transform a positive integer in the following way: change even digits (0, 2, 4, 6, 8) to odd (1, 3, 5, 7, 9), by adding 1; change odd digits (1, 3, 5, 7, 9) to even (0, 2, 4, 6, 8), by subtracting 1. For example, the function would transform the number 2349 into the number 3258. The function should return a -1 if num is less than zero, and otherwise return the transformed number. And the function should print two messages to the screen, indicating the transformation, and the number of digits transformed. For example:
2349 transformed is 3258 these numbers contain 4 digits
Page 5 of 6
Page 6 of 6