COMP1604 COMPUTER SCIENCE, AUTUMN EXAMINATIONS 2003 SOLUTIONS 1.(a) True (b) False (comments are optional in C) (c) False (logic errors are not detected by the C compiler) (d) value of w is 6 (e) value is 2.24 (f) error case (g) condition is false (h) i is 6 i is 1 (i) default default 2 3 default (j) correct answer is (j-3) it returns an integer value to the calling function. (k) correct answer is (k-1) fname() takes 1 argument of type pointer-to-int and returns a value of type float. (l) result is -16 (m) correct answer is (m-2) arrints[0]+arrints[1] (n) True (o) element number 1 is 1 element number 2 is 2 element number 3 is 3 element number 4 is 0 element number 5 is 0 (p) correct answer is (p-3) y = *ptr1; (q) x is -1 and y is -2 (r) False (the name of a string is treated by the C compiler as a fixed pointer-to-char whose value is the address of the first array element and which cannot be reassigned) (s) string=YbcdYfghYjklmn (t) correct answer is (t-2) if it exists, open datafile.txt for reading only, otherwise return an error. 2.(a) int i; char string[100]; /* assume string[] is somehow filled with letters, digits, etc */ for (i=0; string[i]!='\0'; i++){ if ((string[i] >= 'a') && (string[i] <= 'z')){ printf("%c", string[i]); } } (b) if (price>=20){ printf("Sell\n"); } else if (price>=10){ printf("Hold\n"); } else { printf("Buy\n"); } (c) int numneg(int array[], int size){ int i, k=0; /* i is a local loop counter */ for (i=0; i