Combining Macros
Completed
Define MIN(a, b) and MAX(a, b) macros, then CLAMP(x, lo, hi) built from them. Read lo, hi and n values; print each value clamped to [lo, hi].
Input
Line 1: lo hi. Line 2: n. Line 3: n integers.
Output
One line.
Example
Input
2 10 5 0 3 10 11 7
Output
2 3 10 10 7
Editor
C
Hints
Macros can use other macros.
Clamping means: at least lo and at most hi.
Solution explanation
Macros are expanded textually, so CLAMP becomes one big conditional expression.
Show solution
#include <stdio.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define CLAMP(x, lo, hi) MIN(MAX((x), (lo)), (hi))
int main() {
int lo, hi, n, x;
scanf("%d %d %d", &lo, &hi, &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
printf("%d%s", CLAMP(x, lo, hi), i < n - 1 ? " " : "\n");
}
return 0;
}