//Andrew Alspaugh CS1A Chapter 9. P. 539. #10
/*****************************************************************************
Reverse Order of Elements
______________________________________________________________________________
This program was created to showcase a function that reverses the order of
elements in the function.
The program lets the user enter a size of an array, and enter values for the
elements of the array. The program then uses the ReverseElements function to
reverse the order of elements.
The program displays both the original and the reversed array.
_______________________________________________________________________________
//Data Dictionary
//Inputs:
int size;
//Outputs:
int count;
//dynamic functions
// int *array
// int *reversed
******************************************************************************/
#include <iostream>
using namespace std;
int *ReverseElements(int *array, int size);
int main()
{
//Data Dictionary
//Inputs:
int size;
//Outputs:
int count;
//dynamic functions
// int *array
// int *reversed
//INPUT
cout << "Enter number of elements: " << endl;
cin >> size;
//Create Dynamically Allocated Arrays
int *array = new int[size];
int *reversed = new int[size];
//Input values to array
cout << "Enter a value for each element: " << endl;
for (int count = 0; count < size; count++)
cin >> *(array + count);
//PROCESS
//Reverse Elements in array and produce array named reverse
reversed = ReverseElements(array, size);
//OUTPUT
//output original array
cout << "The original array is: " << endl;
for(int count = 0; count < size; count++)
{
cout << *(array + count) << " ";
}
cout << endl << endl;
//output reversed array
cout << "The reversed array is: " << endl;
for(int count = 0; count < size; count++)
{
cout << *(reversed + count) << " ";
}
//delete dynamic arrays after use
delete [] array;
delete [] reversed;
return 0;
}
//This function reverses the contents in an array
int *ReverseElements (int *array, int size)
{
int *reversed = new int[size];
for(int count = 0; count < size; count++)
{
reversed[count] = array[(size - 1) - count];
}
return reversed;
}