I want to create a function which will take a number and allocate an array which will be 'filled' with its divisors. Now, after the line: cout << number << ' ';
the program crashes. I can't understand why. Can anyone help me here? Thanks in advance.
void func(int n)
{
int *arr = 0, number;
GetDivisors(n, arr, number);
cout << number << ' ';
for(int i = 0; i < number; i++)
cout << arr[i];
delete arr;
}
void GetDivisors(int n, int *divisors, int &n_elem)
// There may be a more efficient way of doing this, but this does the job good enough
{
n_elem = 0;
for(int i = 1; i <= n; i++)
if(n % i == 0)
n_elem++;
divisors = newint[n_elem];
n_elem = 0;
for(int i = 1; i <= n; i++)
{
if(n % i == 0)
{
divisors[n_elem] = (n / i);
n_elem++;
}
}
}