//Dione Torrevillas CS1A Chapter 9, P. 537, #1
/*
********************************************************************
ARRAY ALLOCATOR
Description: Create a function that dynamically allocates an array
of integers and returns a pointer to the new array.
INPUT: Number of elements and the values to store in the array.
OUTPUT: The values stored in the dynamically allocated array.
********************************************************************
*/
#include <iostream>
using namespace std;
// Function prototype
int *allocateArray(int);
int main()
{
int size;
cout << "How many integers? ";
cin >> size;
// Make sure the array size is valid.
while (size <= 0)
{
cout << "Enter a number greater than 0: ";
cin >> size;
}
int *numbers = allocateArray(size);
// Store the user's numbers in the allocated array.
for (int i = 0; i < size; i++)
{
cout << "Enter number " << i + 1 << ": ";
cin >> *(numbers + i);
}
cout << "Numbers in the array: ";
for (int i = 0; i < size; i++)
cout << *(numbers + i) << " ";
cout << endl;
// Free the dynamically allocated memory.
delete [] numbers;
numbers = 0;
return 0;
}
// Create an integer array and return a pointer to it.
int *allocateArray(int size)
{
return new int[size];
}