Math 211 Test 2, April 9, 1997 Pink version. Instructions. Please enter your AFS-id and ID number. Problems 1 to 8 worth 10 points each. With the input specified for the given problem, give your output in EXACTLY the same way the computer will generate; the output should be three lines or less, so you should have enough space for each problem. Problem 9 worth a total of 20 points. Each sub problem worth 4 points, there is no partial credit for sub problems. Problem 1: no input #include #define N 6 main() { int a[N] = {2, 2, 3, 3, 4, 4}; int j, k; for ( j = 1; j < 6; j++){ printf("%d ", a[j]); } k = 0; for ( j = 0; j < 6; j++){ k += a[j]; } printf("\n"); printf("%d\n", k/N); return 0; } Problem 2: no input #include main() { int j = 17, k = 23; int *p, *q; p = &k; printf("The first value is %d \n", *p); q = p; p = &j; *p += *q; printf("The second value is %d \n", *p); return 0; } Problem 3: INPUT University of Notre Dame\n #include #include #define SIZE 200 main() { char message[SIZE]; char *p; printf("Enter a message: "); p = message; for ( *p = getchar(); *p != '\n'; ){ *(++p) = getchar(); } for ( p = message; *p != '\n'; p++){ putchar(*p); } printf("\n"); for ( p = message; *p != '\n'; p++){ putchar(toupper(*p)); if ( *p == ' ') break; } printf("\n"); return 0; } Problem 4: no input #include #include main() { char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; char mess[500]; printf("%s\n", days[0]); strcpy( mess, days[1] ); puts(mess); strcat( mess, " " ); strcat( mess, *(days+3) ); strcat( mess, " " ); strcat( mess, days[5] ); puts(mess); return 0; } Problem 5: The program is compiled by the command cc -o tellme problem5.c The user type the following command: tellme how Here is the code problem5.c #include int strcount(char const * const str); main(int argc, char *argv[]) { int j, k; for ( j = 0; j < argc; j++){ k = strcount( argv[j] ); printf("The word"); printf(" \"%s\" ", argv[j]); printf("contains %d characters", k); printf(" (including spaces)\n"); } return 0; } int strcount(char const * const str) { int j; for (j = 0; j < 500; j++){ if (str[j] == '\0') return j; } } Problem 6: no input #include int k = 2; int modify(int j); main() { int j = 2, m = 4; j = modify(j); printf("j is now %d\n", j); m = modify(m); printf("m is now %d\n", m); k = modify(k); printf("k is now %d\n", k); return 0; } int modify(int j) { k += 3; j *= 3; return j; } Problem 7: no input #include #define AREA_T(x,y) (x)*(y)/2 #define AREA_R(x,y) (x)*(y) #define AREA_C(x,y) 3.14*(x)*(x) #define TRIANGLE 0 #define RECTANGLE 1 #define CIRCLE 1 #define AREA(x,y) ( TRIANGLE ? AREA_T(x,y): \ RECTANGLE ? AREA_R(x,y): \ AREA_C(x,y) ) main() { float x = 2.0, y = 5.0; printf("The area is %.2f\n", AREA_C(x,y)); printf("The area is %.2f\n", AREA(x,y)); return 0; } Problem 8: INPUT 6\n #include typedef int Bool ; void modify(int a[], int n); void scrmsg(int b); main() { int a[] = {1, 1, 2, 2, 2}; int k, b = 0; int *p; printf("Please add the following numbers: "); for ( p = a; p < a + 4; p++){ printf("%d ", *p); b += *p; } printf(" = "); p = &k; scanf("%d", p); scrmsg(!(*p - b)); } void scrmsg(Bool b) { b? printf("Great, no mistakes!\n") : printf("Oops, there are mistakes!\n"); } Problem 9. Give the answers The following problem is a modification of Page 182, number 8. (1) Which of the following is a valid declaration of a function that returns nothing and has one float variables? (NO CREDIT will be given if more than two answer is given. If there are more than two correct answers, choose either one of the correct answers). (a) void f(int x); (b) void f(float); (c) void f(x); (d) f(float x); The following problem is a modification of Page 235, number 7. (2) #define TRUE 1 #define FALSE 0 int a[6]; int *p; p = a; The above assignments have been made. Which of the following statement would return TRUE? (NO CREDIT will be given if more than two answer is given. If there are more than two correct answers, choose either one of the correct answers). (a) p == a[0]; (b) p == &a; (c) *p == a[0]; (d) p[0] == a; The following problem is a modification of Page 269, number 2. (3) char *p = "abc"; The above assignments have been made. Which of the following is legal? (NO CREDIT will be given if more than two answer is given. If there are more than two correct answers, choose either one of the correct answers). (a) putchar(p); (b) putchar(*p); (c) printf("%s", *p); (d) puts(*p); (4) int i,j; int a[6]; int *p, *q ; The above assignments have been made. Which of the following is illegal? (NO CREDIT will be given if more than two answer is given. If there are more than two correct answers, choose either one of the correct answers). (a) p = &i; (b) q = *(a+3); (c) p = &a[2]; (d) *q = *p; (e) *p = *&j; (5) #define FALSE 0 #ifdef FALSE #define M 10 #else #define M 80 #endif Answer M =