#include <stdio.h>
int f(int x) {
int result;
if (x >= -2 && x < 2) {
result = x * x;
}
if (x >= 2) {
result = x * x + 4 * x + 5;
}
if (x < -2) {
result = 4;
}
return result;
}
int main() {
int n;
scanf("%d", &n);
printf("%d", f(n));
return 0;
}
This code defines a piecewise function f(x) that returns different values based on the input x. It reads an integer from the user, passes it to f, and prints the result.
- The function uses three separate if statements, but they are not connected with else. This means that for some values of x, more than one if block could execute, and the final result will be the last one that runs. Check what happens when x is exactly -2 or 2.
- Consider whether the conditions overlap at the boundaries. For example, when x is 2, both the first condition (x >= -2 && x < 2) and the second condition (x >= 2) are evaluated. Which one should actually apply?