//Diego Martinez CSC5 Chapter 9, P.537, #1
/*******************************************************************************
* ALLOCATE DYNAMIC INTEGER ARRAY
* ______________________________________________________________________________
* This program dynamically allocates an array of integers based on user input.
* It uses a function to create the array in memory and returns a pointer to it
* for use in the main program.
*
* Computation is based on the Formula:
*
* ptr = new int[size];
*
*______________________________________________________________________________
* INPUT
*
* size (number of elements to allocate in the array)
* user-entered values for each array element
*
* OUTPUT
*
* display of the dynamically allocated array contents
*
*******************************************************************************/
#include <iostream>
using namespace std;
// Function prototype
int* allocateArray(int size);
int main()
{
int size;
// Ask user for number of elements
cout << "Enter number of elements to allocate: \n";
cin >> size;
// Dynamically allocate array
int* arr = allocateArray(size);
// Fill and display array
for (int i = 0; i < size; i++)
{
cout << "Enter value for element " << i + 1 << ": ";
cin >> arr[i];
}
cout << "\nArray contents: ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
// Free memory
delete[] arr;
return 0;
}
// Function that allocates array dynamically
int* allocateArray(int size)
{
int* ptr = new int[size];
return ptr;
}