COMP1604 COMPUTER SCIENCE, AUTUMN EXAMINATIONS 2004 SOLUTIONS 1.(a) True (b) True (c) False (e.g. could have run-time errors) (d) False (must start with a letter or _) (e) first (f) condition is false (g) i is 9 i is 4 i is 1 i is 0 (h) Syntax errors, run-time errors, and logic errors. (i) correct answer is (i-2) it returns a floating-point value to the calling function. (j) correct answer is (j-1) fn() takes 1 argument of type int and does not return a value. (k) result is 4 (l) correct answer is (l-3) arr[0]*arr[1] (m) False (compiles ok, up to programmer to not access memory locations that are not part of the array) (n) a is 3, b is 3 (o) x is 16 and y is 4 (p) value is 4.3 (q) True (r) I-hope-to-do-well-on-this-Exam. (s) False (other ways include end-of-file indicator and First line tells the number of data “records” that follow). (t) correct answer is (t-1) emp1.id_number = 2123; 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] == '0'){ printf("%c", string[i]); } } (b) int v; /* suppose a value is now entered for v - code not shown */ switch(v) { case 5: printf("positive\n"); break; case -5: printf("negative\n"); break; case 0: printf("zero\n"); break; default: printf("incorrect value entered\n"); break; } (c) int num_pos(int array[], int size){ int i, k=0; /* i is a local loop counter */ for (i=0; i0){ k++; } } return (k); } 3.(a) LINE 1: scanf("%d", intarr+i); LINE 2: total += *(intarr+i); (b) #include "stdio.h" void main(void){ char message[80]="I love C programming"; int c_counter(char str[]); /* function prototype */ /* or: put c_counter() before main() */ printf("\"%s\" contains %d c's\n", message, c_counter(message)); } int c_counter(char s[]){ /* no need to pass in size of array */ int i=0, ct=0; /* local variables to c_counter() */ while (s[i]!='\0'){ if ((s[i]=='c') || (s[i]=='C')){ ct++; } i++; } return ct; }