/******************************************************************************
 * Reorder array contents
 * 
 * This Program receives an set of values and stores them within an array, then
 * reverses the order of the values within a new array.
 * 
 * Input:
 *  array
 * 
 * Output:
 *  reversed array
 *****************************************************************************/

#include <iostream>

using namespace std;

// Function Prototype
int* reverseCopy(const int arr[], int size);
void printArray(const int arr[], int size);

int main() {
    const int SIZE = 7;
    int original[SIZE] = {10, 20, 30, 40, 50, 60, 70};

    int* reversed = reverseCopy(original, SIZE);

    cout << "Original array: ";
    printArray(original, SIZE);

    if (reversed != nullptr) {
        cout << "Reversed copy:  ";
        printArray(reversed, SIZE);

        // Clean up dynamically allocated memory
        delete[] reversed;
        reversed = nullptr;
    }

    return 0;
}

// Dynamically allocates a new array in reverse order
int* reverseCopy(const int arr[], int size) {
    if (arr == nullptr || size <= 0) {
        return nullptr;
    }

    int* copy = new int[size];
    for (int i = 0; i < size; ++i) {
        copy[i] = arr[size - 1 - i];
    }

    return copy;
}

// Helper function to display array elements
void printArray(const int arr[], int size) {
    for (int i = 0; i < size; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}