Let's say you have an array with 3 elements in it, {5, 2, 4}.
How would you "manually" reverse these elements?
Something like this:
1 2
|
swap(arr[0], arr[2]);
swap(arr[1], arr[1]); // does nothing, but important to see a pattern
| |
Now let's make it bigger, with an even number of elements: {5, 2, 4, 3}; (size = 4)
1 2
|
swap(arr[0], arr[3]);
swap(arr[1], arr[2]);
| |
Okay, more: {5, 2, 4, 3, 9, 8, 7}; (size = 7)
1 2 3 4
|
swap(arr[0], arr[6]);
swap(arr[1], arr[5]);
swap(arr[2], arr[4]);
swap(arr[3], arr[3]);
| |
One more: {5, 2, 4, 3, 9, 8, 7, 12}; (size = 8)
1 2 3 4
|
swap(arr[0], arr[7]);
swap(arr[1], arr[6]);
swap(arr[2], arr[5]);
swap(arr[3], arr[4]);
| |
See a pattern?
You should write a for loop that swaps elements
r[i] with
r[n-1-i].
where
n is the size of the array, that loops while i < n/2 (integer division, rounded down).
for (int i = 0; i < n/2; i++) { ... }
Try to program it.
For swap,
#include <algorithm>
PLEASE: Use code format tags when posting code. Edit your post and add
[code][/code] around your code.