CS 200 - Lab 26

Reverse a sequence using pointers

This lab is similar to Lab 16: you will implement a function to reverse the values stored in an array of ints.

However, the implementation of your reversal algorithm must use pointers to access and update elements in the array, rather than using the array subscript operator (square brackets).

You will need to add code to the reverse function.  You won't need to modify the main function.

Example run (user input in bold red):

Enter 10 int values:
1 2 3 4 5 6 7 8 9 10
Reversed sequence:
10 9 8 7 6 5 4 3 2 1

Getting started

If you have a solution open in Visual Studio, close it (File->Close Solution).

Download lab26.zip.  Import it into Visual Studio (File->Import...).

Add your code to the source file called Reverse.c.

Hints

Use two pointer variables.  One starts out pointing to the first element of the array, the other points to the last element of the array.  Use a loop to repeatedly swap the values of elements pointed-to by the two pointers, using pointer arithmetic to move the pointers towards each other.

You may wish to write a function to swap the values of two int variables whose addresses are passed as pointers to the function: i.e.:

void swap(int *p1, int *p2) {
/* swap contents of *p1 and *p2 ... */
}