Dynamically allocating an array in array

I am trying to write a function that asks the user how many numbers they want to create and then dynamically allocating an array of that may elements. Then fill each element with a random number. I am not sure how to dynamically allocate an array in a function. I know arrays need constants but I am not sure how to do it dynamically. If anyone can help me I would appreciate it.

Thanks,
R.

Here is the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include<cstdlib>
#include<time.h>

using namespace std;
int main()
{

	srand((unsigned)time(NULL));


F1();

	system("pause");
	return 0;
}

  void F1()
{
	int nums;
	cout << "How many numbers do you want to create?" << endl;
	cin >> nums;

	for(int cnt = 1; cnt < nums; cnt++)
	{
		int array[cnt] = rand();
		
	}


}
1
2
3
4
5
6
7
8
9
10
int *array;

// allocate 100 ints (0-99 just like a array)
array = new int[100];

.... do what you need to do

// free the memory.
delete [] array;
Last edited on
Thanks Softrix for your reply. This will help me figure out the rest.
Thanks
R.
Your welcome :)
Topic archived. No new replies allowed.