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

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
   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.
Completely untested, but how about:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
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.