From 3aafb02dc4ab079fc58b555a6808f302539c7a49 Mon Sep 17 00:00:00 2001 From: Matt Strapp Date: Sat, 25 Sep 2021 16:00:51 -0500 Subject: Start hw1 --- csci5271/hw1/hw1p4.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 csci5271/hw1/hw1p4.c (limited to 'csci5271/hw1/hw1p4.c') diff --git a/csci5271/hw1/hw1p4.c b/csci5271/hw1/hw1p4.c new file mode 100644 index 0000000..e449de5 --- /dev/null +++ b/csci5271/hw1/hw1p4.c @@ -0,0 +1,27 @@ +#include + +/* 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, 0, 9); + for (int i = 0; i < 10; i++) { + printf("%d ", a[i]); + } + printf("\n"); + return 0; +} \ No newline at end of file -- cgit v1.2.3