Two macros in this program give wrong answers. Fix them.
What to build
Corrected macros, with no change to main.
Requirements
- Fix
SQUAREandMINso that they work inside larger expressions. - Do not change
main.
Input
One line with two whole numbers.
Output
Three lines, printed by the starter.
Constraints
Each number is from -100 to 100.
Example
Input:
2 3
Output:
25
96
3
Why it works
A macro's text is pasted in as it stands, so operators in the arguments and around the macro can combine in the wrong order. Brackets fix the grouping.
Examples
Example 1 · Input
2 3
Expected output
25 96 3
Example 2 · Input
5 1
Expected output
36 75 2
Hints
Hint 1
Try SQUARE(a + b) by hand, replacing the text.
Hint 2
Put brackets around each argument and around the whole macro.
Hint 3
MIN has the same problem as SQUARE.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int main(void) {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", SQUARE(a + b));
printf("%d\n", 100 - SQUARE(a));
printf("%d\n", MIN(a, b) + 1);
return 0;
}