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