### Thread: Working with 2-dimensional arrays

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2013
Posts
8
Rep Power
0

#### Working with 2-dimensional arrays

Hello everyone and Happy Thanksgiving to all! I have a quick question in my C-program regarding 2-dimensional arrays (or a matrix if you will). We just learned in my intro C-programming course about 2-dimensional arrays and how they can be thought as an array of pointers. My particular question involves how to manipulate certain cells in a matrix through the use of a function, pass those results back to the main function, and be able to manipulate them further (the changes will be made through user inputs in an infinite for loop until they decide to stop making changes).

My matrix is a 2-dimensional character array, so it has been initialized by the following code:
Code:
`char **gameBoard;`
I have filled my matrix through the use of calloc and nested for loops by the following code:
Code:
``` int i,j,k;

/*Here, we are dynamically allocating memory for our 2d array,
the game board, of size 20x20 with the use of calloc.*/
gameBoard = calloc(gameBoardSize, sizeof(char *));
for(i=0; i<gameBoardSize; ++i)
gameBoard[i] = calloc(gameBoardSize, sizeof(char));

int percentChanceGold;
int percentChancePit;

/*Filling the game board with either a blank space, gold, or a
pit. The chances of having gold in a room is 10% while the chance
r
of having a pit is 20%. Since a room cannot have both gold and
a pit, if the number generator has a result as such, the placement
of gold takes priority of a pit.*/

for(j=0; j<gameBoardSize; ++j)
for(k=0; k<gameBoardSize; ++k)
{
if(j==(gameBoardSize-1)&&k==0)
{
gameBoard[j][k]='X';
}
else
{
percentChanceGold=rand()%101;
percentChancePit=rand()%101;

if(percentChanceGold>=90&&percentChancePit<80)
{
gameBoard[j][k]='G';
}
else if(percentChanceGold<90&&percentChancePit>=80)
{
gameBoard[j][k]='P';
}
else if(percentChanceGold>=90&&percentChancePit>=80)
{
gameBoard[j][k]='G';
}
else
{
gameBoard[j][k]='_';
}
}
}```
I'm trying to write a function that will make changes to the matrix based on user input, and I'm not sure I know the proper syntax. Normally, I would think I would call on a pointer of a variable, but since this is an array of pointers, I'm not sure I know the syntax (***gameBoard doesn't seem right) to call on the gameBoard and modify its contents. Any help would be much appreciated, and thank you in advance for taking the time to read this.
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2013
Posts
8
Rep Power
0
To give you an idea of what I have so far,
Function Prototype
Code:
`void movePlayerUp(char **gameBoard, int gameBoardSize);`
Code:
```void movePlayerUp(char **gameBoard, int gameBoardSize)
{
int j,k;
int *previousRowLocation;
int *previousColumnLocation;

for(j=0; j<gameBoardSize; ++j)
{
for(k=0; k<gameBoardSize; ++k)
{
if(gameBoard[j][k]='X')
{
previousRowLocation=&j;
previousColumnLocation=&k;
break;
}
break;
}
break;
}

gameBoard[*previousRowLocation][*previousColumnLocation]='_';
gameBoard[(*previousRowLocation-1)][*previousColumnLocation]='X';
}```
What I'm trying to accomplish is having a char 'X' move up one row through this function
3. > if(gameBoard[j][k]='X')
Try using == rather than = for comparison

> previousRowLocation=&j;
> previousColumnLocation=&k;
Make a copy of the values in integer variables.
You've just made an obscure way of following the current values of j and k.
You want to preserve the values.

If you want to avoid using goto, breaking out of a nested loop needs something like this.
Code:
```bool found = false;
for(j=0; j<gameBoardSize && !found; ++j)
{
for(k=0; k<gameBoardSize && !found; ++k)
{
if(gameBoard[j][k]=='X')
{
found = true;```