diff options
author | Matt Strapp <matt@mattstrapp.net> | 2022-05-24 11:18:46 -0500 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2022-05-24 11:19:55 -0500 |
commit | 7a73162607544204032aa66cce755daf21edebda (patch) | |
tree | 58578e01f15f34a855d99c32898db9d7a1603e67 /csci5271/hw1/hw1p4.c | |
parent | do some stuff (diff) | |
download | homework-7a73162607544204032aa66cce755daf21edebda.tar homework-7a73162607544204032aa66cce755daf21edebda.tar.gz homework-7a73162607544204032aa66cce755daf21edebda.tar.bz2 homework-7a73162607544204032aa66cce755daf21edebda.tar.lz homework-7a73162607544204032aa66cce755daf21edebda.tar.xz homework-7a73162607544204032aa66cce755daf21edebda.tar.zst homework-7a73162607544204032aa66cce755daf21edebda.zip |
Graduate
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to 'csci5271/hw1/hw1p4.c')
-rw-r--r-- | csci5271/hw1/hw1p4.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/csci5271/hw1/hw1p4.c b/csci5271/hw1/hw1p4.c new file mode 100644 index 0000000..4526e43 --- /dev/null +++ b/csci5271/hw1/hw1p4.c @@ -0,0 +1,32 @@ +#include <stdio.h> + +/* Reverse the elements from FROM to TO, inclusive */ +void reverse_range(int *a, int from, int to) +{ + unsigned int *p = &a[from]; + unsigned int *q = &a[to]; + /* Until the pointers move past each other: */ + while (!(p == q + 1 || p == q + 2)) + { + /* Swap *p with *q, without using a temporary variable */ + *p += *q; /* *p == P + Q */ + *q = *p - *q; /* *q == P + Q - Q = P */ + *p = *p - *q; /* *p == P + Q - P = Q */ + /* Advance pointers towards each other */ + p++; + q--; + } +} + +int main() +{ + int a[10] = {255, 0, -65536, 2147483647, -2147483648, + -1, 0, 1, 2, 3}; + reverse_range(a, 9, 0); + for (int i = 0; i < 10; i++) + { + printf("%d ", a[i]); + } + printf("\n"); + return 0; +}
\ No newline at end of file |