diff options
author | Matt Strapp <matt@mattstrapp.net> | 2021-09-25 16:18:39 -0500 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2021-09-25 16:18:39 -0500 |
commit | 604ae9bc9e9c7fbda3b192f5a925c70a970d6962 (patch) | |
tree | 89269f8f68a428050a176d064aa742425e0c928d /csci5271/hw1/hw1p4.c | |
parent | run formatter (diff) | |
parent | Start hw1 (diff) | |
download | homework-604ae9bc9e9c7fbda3b192f5a925c70a970d6962.tar homework-604ae9bc9e9c7fbda3b192f5a925c70a970d6962.tar.gz homework-604ae9bc9e9c7fbda3b192f5a925c70a970d6962.tar.bz2 homework-604ae9bc9e9c7fbda3b192f5a925c70a970d6962.tar.lz homework-604ae9bc9e9c7fbda3b192f5a925c70a970d6962.tar.xz homework-604ae9bc9e9c7fbda3b192f5a925c70a970d6962.tar.zst homework-604ae9bc9e9c7fbda3b192f5a925c70a970d6962.zip |
Merge branch 'master' of git.mattstrapp.net:RossTheRoss/Homework
Diffstat (limited to '')
-rw-r--r-- | csci5271/hw1/hw1p4.c | 27 |
1 files changed, 27 insertions, 0 deletions
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 <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, 0, 9); + for (int i = 0; i < 10; i++) { + printf("%d ", a[i]); + } + printf("\n"); + return 0; +}
\ No newline at end of file |