From 70d8d3dc17402dc81d9f049721cc446d2e2a5029 Mon Sep 17 00:00:00 2001 From: Jason Pochanke Date: Wed, 7 Oct 2026 21:37:05 +0200 Subject: [PATCH] lesson 8 clamp it --- main.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index f356125..7b5ce25 100644 --- a/main.c +++ b/main.c @@ -1,16 +1,25 @@ #include +/* Return value limited to the range lo..hi (lo <= hi is guaranteed). */ +int clamp(int value, int lo, int hi) { + /* TODO: return lo, hi, or value itself, whichever applies. */ + int result = value; + if (result > hi) { + result = hi; + } else if (result < lo) { + result = lo; + } + return result; +} + int main(void) { - int n; - scanf("%d", &n); - long long result = 0; - long imat = 1; - while (!(imat>n)) { - result = result +imat; - imat = imat+2; - - } - - printf("%lld", result); - return 0; + int lo, hi, n; + scanf("%d %d", &lo, &hi); + scanf("%d", &n); + for (int i = 0; i < n; i++) { + int x; + scanf("%d", &x); + printf("%d\n", clamp(x, lo, hi)); + } + return 0; }