//Amari Mosley CSC5 Chapter 9, P. 537 #1
 
/********************************************************************
 *
 * Allocate Array
 * _______________________________________________________________
 *
 * This program dynamically allocates an array of integers and
 * returns a pointer to the dynamically allocated array.
 *
 *
 * _______________________________________________________________
 *
 * INPUT
 *        numElements       : Number of integers to allocate
 * OUTPUT
 *        arrayPtr          : Pointer to the dynamically allocated array
 *
 ********************************************************************/
#include <iostream>
using namespace std;
 
// Function Prototype
int* allocateArray(int);
 
int main()
{
    // INPUT
    int numElements;
 
    // Initialize Program variables
    int* arrayPtr;
 
    // Get the number of elements from the user
    cout << "Enter the number of elements to allocate: ";
    cin >> numElements;
 
    // Dynamically allocate the array
    arrayPtr = allocateArray(numElements);
 
    // Output Result
    if (arrayPtr != nullptr)
    {
        cout << "Array of " << numElements
             << " integers was successfully allocated." << endl;
 
        // Free dynamically allocated memory
        delete[] arrayPtr;
    }
    else
    {
        cout << "Invalid number of elements." << endl;
    }
 
    return 0;
}
 
// Function to dynamically allocate an array of integers
int* allocateArray(int numElements)
{
    if (numElements <= 0)
    {
        return nullptr;
    }
 
    return new int[numElements];
}