C Programming - APS106 - Mid - 2005
C Programming - APS106 - Mid - 2005
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");
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
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