Need help with random numbers in arrays

closed account (EAp4z8AR)
I have to make a 2D array that is 10*10 and need to store 50 "x"s in random spots. As of now I have a 2D array that fills in 100 random numbers but how would I change it to store "x"s randomly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 int main()
{
    srand((unsigned) time(0));
    const int size = 10;
    int array[size];
    
    for (int i=0; i<size; i++)
    {
        for (int j=0; j<size; j++)
        {
            array[i] = (rand()%100+1);
            cout << array[i] << " ";
        }
        cout << "\n";
    }
}
Last edited on
closed account (EAp4z8AR)
I understand that I need to change the int array to char array. And that I also need to set array[i] = 'x' but I just don't get how to generate x in 50 random spots.
But you're using a one dimensional array..? I'll try to help in a moment
Hello vmansuria,

First off "size" should be in all capital letters to let you know it is defined as a constant.

You should define line 5 as a 2D array. The 1D array to figure this out is fine, but you will have to start all over when changing it to a 2D array. Might as well deal with it now. Also you can change it to a character array.

Your for loops have the right idea except instead of "i" and "j" I would call them "row" and "col" it helps you understand better what is going on.

On line 11 what it should be is array[][] = 'x';. Normally you would use "row" and"col" in the []s, but in this case you will need two new variables for the []s. These will be the result of generating a random number with in the constraints of the array size. This will place an "x" at random points in the array.

Hope that helps,

Andy
As the name suggests (Nwb), I'm a newbie but I'll try to help with what I could come up with.

It looks like you need help with the logic, so let's work on that. If you need help putting together the program then I can help with that too. (however I will mention the programming parts)

First let's assume the 2D array we're going to be using is 'array'.
So we initialize array to char array[10][10];

This is the correct way to fill the 2D array with arbitrary elements:
1
2
3
        for(i=0; i<10; i++)
                for(j=0; j<10; j++)
                        array[i][j] = itoa((rand()%100+1))          
- itoa converts integer to character

So now that we've done that, let's work on the 'x'!

So we need to fill an 'x' 50 times inside the array. So let's write a while loop that makes sure the filling happens 50 times.

First initialize an iterating variable: int filled_x = 0; - this tells us that 0 'x' have been filled so far

Now the while loop:
while(x<50) {} - loop until 50 'x' have been filled.

Inside this loop now, we can start filling elements. So we do that by generating rowth and columth and assigning that position in 2D array: 'x'. Simple right? Ah but what if the generator generates the same rowth and columnth two times? Then the while loop thinks that there are two 'x' but in reality there is only one 'x'.

So let's make sure that the 'x' is replacing one of the arbitrary numbers we filled in the 2D array earlier and not another 'x'.

First lets generate some rowth and columnth value.
int row = rand(/*0 to 9*/)

int column = rand(/*0 to 9*/)
You can declare these at the beginning if you want. (note that I said "declared", you still have to assign it to rand here)

Now, let's use another while loop to find a 2D value which isn't occupied by an 'x'.

Something like this:
while(array[row][column] == 'x') {} - this means that the loop will persist as long as pointed to value is an 'x'.

So what's inside the while loop? Well, this is:
1
2
3
4
5
 
{
int row = rand(/*0 to 9*/)
int column = rand(/*0 to 9*/)
}


So it will find a new row and column when the pointed to value is an 'x'. Eventually you will find a value that is not an 'x' and you can insert your x here.

So after the while loop,
1
2
filled_x++
array[row][column] = 'x';



That's the best approach I could come up with, maybe somebody can come up with something more efficient but in practice you will get the same solution and regardless computers operate so quick that you wouldn't be able to notice the difference itself.

Anyways hope that helps and good luck! :D
Last edited on
closed account (EAp4z8AR)
Handy Andy, the way you suggested still fills in the entire array with 'x'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    srand((unsigned) time(0));
    const int SIZE_X = 10;
    const int SIZE_Y = 10;
    char array[SIZE_X][SIZE_Y];
    
    for (int column=0; column<SIZE_X; column++)
    {
        for (int row=0; row<SIZE_Y; row++)
        {
            int i = rand() % SIZE_X;
            int j = rand() % SIZE_Y;
            
            array[i][j] = 'x';
            
            
            cout << array[i][j] << " ";
        }
    cout << "\n";
    }
    
}


It's more like this:

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
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(0));
    const int NROWS = 10, NCOLS = 10, NUM_XS = 50;;
    char grid[NROWS][NCOLS];

    fill(&grid[0][0], &grid[NROWS][0], '.');

    for (int i = 0; i < NUM_XS; i++) {
        int row, col;
        do {
            row = rand() % NROWS;
            col = rand() % NCOLS;
        } while (grid[row][col] == 'x');
        grid[row][col] = 'x';
    }
    
    for (int r = 0; r < NROWS; r++) {
        for (int c = 0; c < NCOLS; c++)
            cout << grid[r][c] << ' ';
        cout << '\n';
    }
}


Note that you need to initialize (fill) the array.
You need to loop 50 times to place 50 x's.
And you need to keep picking a new point if the current point already has an x.
Topic archived. No new replies allowed.