need basic help with arrays

closed account (1R91hbRD)
I just want generate a random number for each array. why cant I do it this way(why would it not work this way?)?what way can I do it then?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
	srand(time(NULL));
	int generated_nums;
	generated_nums = rand()%9;
	int lottery[4] = generated_nums;

	cout << lottery [0], lottery [1], lottery [2],lottery [4];
return 0;		

}
@zoids1222

Try this way..

I didn't check it, but it should ok

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
	srand(time(NULL));
	int generated_nums, lottery[4];
for(int x=0;x<4;x++)
{	
generated_nums = rand()%9;
	lottery[x] = generated_nums;
}
	cout << lottery [0], lottery [1], lottery [2],lottery [3];
return 0;		
}
closed account (1R91hbRD)
thank you very much :)
Topic archived. No new replies allowed.