// Andres Guzman CSC5 Chapter 9, P. 537, #1
/******************************************************************************
*
* ALLOCATE ARRAY
* ____________________________________________________________________________
* Program will dynamically allocate an array and return a pointer to array.
*
* Computation:
*
* Recieves array size as an argument and proceeds to dynamically allocate to
* the value of array size before returning the memory address of the
* dynamic memory using a pointer.
* ____________________________________________________________________________
*
* INPUT
*
* SIZE = Inputed Array Size
*
* OUTPUT
*
* number = Memory Address of Array
*
******************************************************************************/
#include <iostream>
using namespace std;
int *dma(int a); //Function Prototype
int main()
{
int SIZE; //Variable to hold inputed value
//
//Display Prompt for input
cout << "Please Enter A Value: ";
cin >> SIZE;
while(SIZE <= 0) //Input Validation
{
cout << "Please Enter Positive Value." << endl;
cout << "Please Enter A Value: ";
cin >> SIZE;
}
//
//Calls Functions and adds returned value to variable
int *number = dma(SIZE);
//
//Output results
cout << "\nMemory Address: " << number << endl;
delete [] number;
return 0;
}
/*******************************************************************************
* Adding definition to function. The function will dynamically allocates an
* integer array of the size inputed by the user and return a pointer holding
* the memory address of the allocated array.
******************************************************************************/
int *dma(int a)
{
int * arr = new int[a]; //Dynamically Allocates Memory
return arr; //Returns the value of new memory
}