//Ryan Shahriyarpour CS1A Chapter 9 Question 10
/*****************************************************************************
*
* Reverse an Array
*
*********************
*
* This program makes a copy of an array with the elements in reverse order.
* It displays the original array and the reversed copy.
*
* INPUT
* None (the array is set in the program)
*
* OUTPUT
* The original array and the reversed copy
*
****************************************************************************/
#include <iostream>
using namespace std;
const int SIZE = 6; // Number of elements in the array
// Function prototypes
int *reverseArray(const int *array, int size);
void showArray(const int *array, int size);
int main()
{
// data dictionary
int original[SIZE] = {10, 20, 30, 40, 50, 60}; // The original array
int *reversed; // Pointer to the reversed copy
reversed = reverseArray(original, SIZE);
cout << "Original array: ";
showArray(original, SIZE);
cout << "Reversed copy: ";
showArray(reversed, SIZE);
// Free the memory
delete [] reversed;
reversed = nullptr;
return 0;
}
// Returns a pointer to a new array holding the elements in reverse order
int *reverseArray(const int *array, int size)
{
int *newArray = new int[size]; // Pointer to the new array
int count; // Loop counter
for (count = 0; count < size; count++)
*(newArray + count) = *(array + (size - 1 - count));
return newArray;
}
// Displays the elements of an array on one line
void showArray(const int *array, int size)
{
int count; // Loop counter
for (count = 0; count < size; count++)
cout << *(array + count) << " ";
cout << endl;
}