Midterm 1 Solutions
1. unsigned -i+u 111
unsigned u%9 2
float f+i -5.5
double f*2.0+i -1.0
int i++/3 -3 or -4
double -d+u*2 167.0
int abs(-f-0.5) 5
int i=d 35
float (++i>0)+f 5.5
double f+(u==i)*d 4.5
2. 3F2 1010
100011 04
174 025
170054 0xF02C
111 43
3. Sets x[0..19] to a random permutation of the integers 10..29.
4. 1 int x; x: 1-5, 9-12
2 void f(int a, int b[]) { a: 2-4, 9, 12 b: 2-12
3 x = a + b[0];
4 if (a < 0) {
5 int a = x; a: 5-8
6 int x = b[-a]; x: 6-8
7 while (x < 10)
8 a += b[x++]; }
9 else if (a > 0) {
10 int a = x + 1; a: 10-11
11 x *= b[a] + a; }
12 }
5. Doesn't work if x and y are the same array, e.g., reverse(a, a, n).
int i, j = len - 1;
for (i = 0; i < len; i++)
x[i] = y[i];
for (i = 0; i < j; i++) {
int t = x[i]; x[i] = x[j]; x[j--] = t }
6. for (i = argc - 1; i > 0; i--) {
int j;
for (j = 0; word[j] == argv[i][j] && word[j] != '\0'; j++)
;
if (word[j] == argv[i][j])
break;
}
7. int i = strlen(dst), j = 0;
while ((dst[i++] = src[j++]) != '\0')
;
Copyright © 1996 David R. Hanson / drh@cs.princeton.edu
$Revision: 1.1 $ $Date: 1996/10/11 17:24:20 $