Task:
There is a dynamic BOMB_COLLECTION array, it has 11 instances of the BOMB class.
There are variables in the class: X and Y (coordinates "bombs"). It is required to make the program itself generate random X and Y coordinates.
I am writing this code:
for (unsigned i = 0; i <=sizeof(BOMB_COLLECTION); i++ )//всего 11 итерац. { int _bombX,_bombY; srand(time(NULL)); _bombX = rand()%301; srand(time(NULL)); _bombY = rand()%601; }
Result: in all 11 cases, X and Y are the same: (although each time you start it is different.
And if I write:
for (unsigned i = 0; i <=sizeof(BOMB_COLLECTION); i++ )//всего 11 итерац. { int _bombX,_bombY; _bombX = rand()%301; _bombY = rand()%601; }
Result: we get the coordinates of the first "bomb" (generated normally), the coordinates of the second "bomb" are also generated normally, but all subsequent bombs copy the coordinates of the SECOND!