mirror of
https://github.com/nolongeractivemp3/shipthatcode-c.git
synced 2026-11-24 10:45:12 +01:00
lesson 9 pointers
This commit is contained in:
1 parent
70d8d3dc17
commit
cfb394d1bd
1 file changed
+9
-18
@@ -1,25 +1,16 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdnoreturn.h>
|
||||||
|
|
||||||
/* Return value limited to the range lo..hi (lo <= hi is guaranteed). */
|
void swap(int *a, int *b) {
|
||||||
int clamp(int value, int lo, int hi) {
|
int temp = *a;
|
||||||
/* TODO: return lo, hi, or value itself, whichever applies. */
|
*a = *b;
|
||||||
int result = value;
|
*b = temp;
|
||||||
if (result > hi) {
|
|
||||||
result = hi;
|
|
||||||
} else if (result < lo) {
|
|
||||||
result = lo;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int lo, hi, n;
|
int a, b;
|
||||||
scanf("%d %d", &lo, &hi);
|
scanf("%d %d", &a, &b);
|
||||||
scanf("%d", &n);
|
swap(&a, &b);
|
||||||
for (int i = 0; i < n; i++) {
|
printf("%d %d\n", a, b);
|
||||||
int x;
|
|
||||||
scanf("%d", &x);
|
|
||||||
printf("%d\n", clamp(x, lo, hi));
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user