/*******************************************************************************
* Aiden Stannard // CS1A // Chapter 9 Homework: Challange 10# // 11/21/2026
* _____________________________________________________________________________
*
* Reverse Array
* _____________________________________________________________________________
*
* This program take the values of an array, reverses the order, and outputs the
* values of the original array and its original order, aswell the new array
* with the reverse order.
* _____________________________________________________________________________
*
* Input: int* reversed = reverseArray(original, SIZE);
*
* Output: Original array: 5 10 15 20 25
* Reversed array: 25 20 15 10 5
*
*
******************************************************************************/
#include <iostream>
using namespace std;
// Function prototype
int *reverseArray(const int arr[], int size);
int main() {
const int SIZE = 5;
int original[SIZE] = {5, 10, 15, 20, 25}; // Original function
// Call the function and store the returned pointer
int* reversed = reverseArray(original, SIZE);
// Display the original array
cout << "Original array: ";
for (int i = 0; i < SIZE; i++) {
cout << original[i] << " ";
}
cout << endl;
// Display the reversed array
cout << "Reversed array: ";
for (int i = 0; i < SIZE; i++) {
cout << reversed[i] << " ";
}
cout << endl;
// Free the dynamically allocated memory
delete[] reversed;
reversed = nullptr; // reset the pointer to a null value
return 0;
}
int* reverseArray(const int arr[], int size) {
// Dynamically allocate a new array of the same size
int* newArr = new int[size];
// Copy elements in reverse order
for (int i = 0; i < size; i++) {
newArr[i] = arr[size - 1 - i];
}
// Return the pointer to the new array
return newArr;
}