I'm supposed to dynamically allocate an array of integers. The function should accept an integet argument indicating teh number of elements to allocate. The function should return a pointer to the array.
This is what i got so far.
#include <iostream>
using namespace std;
//Function prototype
int *arrAllocator(int);
int numElements;
int num;
int count; //Loop counter
int main()
{
int *values; //To point to the numbers
//Get an array of number chosen by user.
values = arrAllocator(numElements);
cout << "\nEnter an array size: " << endl;
cin >> numElements;
//Get number values from user.
cout << "Enter the value of the elements below.\n";
for (count = 0; count < numElements; count++)
{
cout << "Value #" << (count + 1) << ": ";
cin >> values[count];
}
//Display the numbers.
for (int count = 0; count < numElements; count++)
cout << values[count] << endl;
//Free the memory.
delete [] values;
values = 0;
return 0;
}
//The function returns a pointer to an array.
int *arrAllocator(int numElements)
{
int *array;
//Invalid if zero or negative.
if (numElements <= 0)
return NULL;
//Dynamically allocate the array.
array = new int[num];
//Return a pointer to the array
return array;
}
The program will start to run normal but after the user inputs the first number in the array it stops for errors. I know that a my mistakes are more than likely in returning the pointer to the array. But i can't figure it out i even wonder if maybe i linked some of the names incorrectly. Could some one please assist in telling me where im messing up, or did i go about writting the program incorrectly. Thank you in advance.