PROBLEM 1: runner% cat exam2_1.c #include void main(void) { int a, b; while (1) { scanf("%i %i", &a, &b); if (a < 0 && b < 0) break; printf("%3i, %3i: Not yet\n", a, b); } printf("%3i, %3i: Th-th-th-that's all folks\n", a, b); } runner% exam2_1 3 6 3, 6: Not yet 0 3 0, 3: Not yet -1 4 -1, 4: Not yet 3 -4 3, -4: Not yet 0 0 0, 0: Not yet -1 -2 -1, -2: Th-th-th-that's all folks /* alternative version */ #include void main(void) { int a, b; scanf("%i %i", &a, &b); while (a >= 0 || b >= 0) { printf("%3i, %3i: Not yet\n", a, b); scanf("%i %i", &a, &b); } printf("%3i, %3i: Th-th-th-that's all folks\n", a, b); } PROBLEM 2: runner% cat exam2_2.c #include void chars(int n, char ch); void main(void) { int n, line; scanf("%i", &n); chars(1, '*'); chars(1, '\n'); for (line = 2; line < n; line ++) { chars(1, '*'); chars(line-2, ' '); chars(1, '*'); chars(1, '\n'); } chars(n, '*'); chars(1, '\n'); } void chars(int n, char ch) { int i; for(i = 0; i < n; i++) printf("%c", ch); } runner% exam2_2 10 * ** * * * * * * * * * * * * * * ********** runner% exam2_2 5 * ** * * * * ***** --------------------------------------------- PROBLEM 3: runner% cat exam2_3.c #include int addup(int n); void main(void) { int n, sum; scanf("%i", &n); sum = addup(n); printf("Sum of integers up to %i == %i\n", n, sum); } int addup(int n) { int i, sum = 0; for(i = 1; i <= n; i++) sum = sum + n; return sum; } runner% cc -o exam2_3 exam2_3.c runner% exam2_3 10 Sum of integers up to 10 == 55 runner% exam2_3 4 Sum of integers up to 4 == 10 ---------------------------------------------- PROBLEM 4: runner% cat exam1_4.c #include #include #include int roll(void); void main(void) { int i, n = 1000; int spots[7] = {0, 0, 0, 0, 0, 0, 0}; srand48((long)time(NULL)); for (i = 0; i < n; i++) spots[roll()]++; for (i = 1; i < 7; i++) printf("Roll of %i, %4i times, %5.2%%\n", i, spots[i], (double)spots[i]/(double)n * 100.0); } int roll(void) { return (int)(6.0*drand48() + 1.0); } runner% cc -o exam2_4 exam2_4.c vip% exam2_4 Roll of 1, 175 times, 17.50% Roll of 2, 168 times, 16.80% Roll of 3, 166 times, 16.60% Roll of 4, 166 times, 16.60% Roll of 5, 165 times, 16.50% Roll of 6, 160 times, 16.00% vip% exam2_4 Roll of 1, 177 times, 17.70% Roll of 2, 181 times, 18.10% Roll of 3, 156 times, 15.60% Roll of 4, 168 times, 16.80% Roll of 5, 157 times, 15.70% Roll of 6, 161 times, 16.10% vip% exam2_4 Roll of 1, 145 times, 14.50% Roll of 2, 176 times, 17.60% Roll of 3, 190 times, 19.00% Roll of 4, 165 times, 16.50% Roll of 5, 164 times, 16.40% Roll of 6, 160 times, 16.00% vip%