EXERCISES ON C FUNCTIONS READING ------- Notes for lecture 2. Kernighan and Ritchie, 1.7, 1.8 Sedgewick, p. 73 King, Chapter 9.1 - 9.5 (Functions) SUPPLEMENTAL READING -------------------- Deitel and Deitel, 5.1-5.5 EXERCISES --------- 1. What is wrong with the following C function? int square(int x); { return x*x; } 2. What is the effect of calling show(4)? int show(int x) { int i; for (i = 0; i < x; i++) printf(%d %d\n", i, i*i); return x*x; for (i = 0; i < x; i++) printf(%d %d\n", i, i*i); return 0; } 2. What does the following C function do? int eq3(int a, int b, int c) { if ((a == b) && (a == c)) return 1; else return 0; } 2. Write a C function that takes two integers as arguments and returns the value of the larger one. 3. Write a C function that takes three integers as arguments and returns the value of the largest one. 4. Write a C function that takes a real number as an argument and returns the absolute value of that number. 5. Write a C function that takes a positive integer N as an argument and returns the largest power of two less than or equal to N. 6. Write a C function that takes a positive integer N as an argument and returns 1 if N is prime, and 0 otherwise. 7. Write a C function that takes a positive integer N as an argument and returns 0 if N is prime, and 1 otherwise. 8. What values are printed out by the following C program? #include int f(int x) { return x+2; } int main(void) { int x = 5; printf("%d %d\n", f(x+2), f(f(x+2))); return 0; } 9. Run the following program through the compiler to see the error messages that gcc (or cc) produces for semicolon errors. int square (int x); { return x*x; } int main(void) { int a, b, c; c = 0 b = 0; if (a > b) c = 0 else b = 0; return 0; } . . . . ANSWERS TO EXERCISES ON C FUNCTIONS 1. Extra semicolon on the first line. Only function prototypes have semicolons after the parenthesis, not function definitions. 2. It returns 16 and prints. 0 0 1 1 2 4 3 9 The function stops as soon as the first return statement is reached. 2. It takes 3 integer arguments and returns 1 (true) if all three are equal, and 0 otherwise. Veteran C program might write this more succintly as the following since the result of the && operation is always 0 or 1. int eq3(int a, int b, int c) { return (a == b) && (a == c); } 2. int max(int a, int b) { if (a >= b) return a; else return b; } The "else" is not essential since once a return statement is reached, no further statements are executed. int max(int a, int b) { if (a >= b) return a; return b; } 3. int max3(int a, int b, int c) { if (a >= b && a >= c) return a; else if (b >= c) return b; else return c; } 4. int absolute_value(double x) { if (x > 0) return x; else return -x; } Alternatively, use the fabs() function from the math library. Be sure to #include . 5. int f(int N) { int j; for (j = 1; j <= N; j += j) ; return j/2; } 6. We take a brute force approach, and check integers less than N to see if they divide evenly into N. int isPrime(int N) { int divisor; if (N <= 1) return 0; for (divisor = 2; divisor < N; divisor++) if (N % divisor == 0) /* N is an integral multiple of divisor */ return 0; return 1; } 7. One solution is to copy the above code and exchange 0 and 1 in the three return statements. A better solution is to call the above function. Recall ! means logical NOT in C. int isNotPrime(int N) { return !isPrime(N); } 8. Prints out "9 11". 9. Different compilers have different ways of trying to tell you about the problems. % cc bad.c "bad.c", line 2: syntax error before or at: { "bad.c", line 2: undefined or not a type: x "bad.c", line 2: warning: old-style declaration or incorrect type for: x "bad.c", line 2: identifier redeclared: x current : pointer to int previous: function(void) returning int : "bad.c", line 2 "bad.c", line 2: syntax error before or at: } "bad.c", line 7: syntax error before or at: b "bad.c", line 8: syntax error before or at: else cc: acomp failed for bad.c % lcc bad.c bad.c:2: unrecognized declaration bad.c:2: unrecognized declaration bad.c:2: syntax error; found `*' expecting `;' bad.c:2: redeclaration of `x' previously declared at bad.c:2 bad.c:2: unrecognized declaration bad.c:7: syntax error; found `b' expecting `;' bad.c:8: syntax error; found `else' expecting `;' bad.c:9: warning: missing return value % gcc bad.c bad.c:2: parse error before `{' bad.c: In function `main': bad.c:7: parse error before `b' bad.c:8: parse error before `else' % Compilers do the best they can to try to understand what we give them to be legal C programs. When we make errors, their interpretation may diverge from our intention, to the point that the error messages may not make sense to us. Lesson: fix the *first* error, then try again.