### Random int array HELP please!

Hi Im trying to create a random 2D array (3 x 3) so that numbers 1-9 will randomly be placed in the grid behind the '?' shown in coveredarray but in the guessarray just wanted to know if im doing right, im faily new to C++, also i wanted to ask how I would go about asking the user to pick what row and column to choose to reveal what numbers behind the '?'

Thanks

 ``123456789101112131415161718192021222324252627282930`` `````` int coveredarray [3][3] = {{'?','?','?'}, {'?','?','?'}, {'?','?','?'}}; for(int row = 0; row < 3; row++) { for(int column = 0; column < 3; column++) { cout << coveredarray[row][column] << " "; } cout << endl; } //This is the Guess Array int guessarray[3][3]; {{'?','?','?'}, {'?','?','?'}, {'?','?','?'}}; *srand(time(0)); // Initialize random number generator. guessarray = (rand() % 3) ; if (guessarray>1 && guessarray <9); for(int row = 0; row <3; row++) { for(int column = 0; column <3; column++) { cout << guessarray[row][column]; } } } ``````
Last edited on
Wait, if you wanted to place the numbers 1-9 shouldn't it be guessarray(rand()%9)+1? This will generate a number from 1-9 every time thus making your 'if' statement useless. On the other hand, there is the problem of the program generating the same number twice. I'm fairly new to cpp as well so I'm just giving my input.

 ``12345678910111213141516171819`` ``````int myarray[3][3]={{0,0,0},{0,0,0},{0,0,0}} int main() { srand (unsigned time()) int numbersentered=0; for (int i=1; i<=9; i++) { while (i>numbersentered) { int x=rand()%3; int y=rand()%3; if (myarray[x][y]==0) { myarray[x][y]=i; numbersentered++; } } } } ``````

As for getting the user to pick one, the simple way is just to do cin's, either a single one going from 1 to 9, or one for row and one for column.
Last edited on
i am fairly new to c++ too, here what I suggest(untested)

 ``12345678910111213`` ``````int main() { int new_array[3][3] = {{0,0,0},{0,0,0},{0,0,0}}; srand(unsigned time(0)); new_array[3][3] = rand()%9 + 1; for(int row = 0; row < 3; row++) for(int col = 0; col < 3; col++) cout << new_array[row][col]; } ``````
Topic archived. No new replies allowed.