aboutsummaryrefslogtreecommitdiffstats
path: root/csci5271/hw1/hw1p4.c
blob: e449de51f24410884991d6071444c327be6d0d42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}