# Thread: Printing 2d array problem

1. ## Printing 2d array problem

Hi, This an odd problem, I am making a connect 5 game with a 15x15 array but after a player enters a row and column the board doesn't print [13][14] or [14][0-6] for some reason. Run this code and you will see. The initial printBoard() works fine but after 1 turn this happens, I can't figure out the problem. Thanks

Code:
```#include <stdio.h>

void printBoard();
void initBoard();

char board[14][14];
int row, col;
int turn = 1;  //1 = player 1 turn, 2 = player 2 turn

int main()
{
initBoard();

while(1)
{
printBoard();
}

return(0);
}

{
if(turn == 1)
printf("%s", "\nPlayer 1: ");
else
printf("%s", "\nPlayer 2: ");

scanf("%d %d", &row, &col);

if(row == 99 && col == 99)  //To end game early enter 99 99
exit(0);

if(board[row][col] == '-' && row < 15 && row >= 0 && col < 15 && col >= 0)
if(turn == 1)
{
board[row][col] = 'O';
turn = 2;
}
else
{
board[row][col] = 'X';
turn = 1;
}

else
printf("%s", "Invalid space, go again.\n");
}

void initBoard()
{
int i,j;
for(i=0; i<15; i++)
for(j=0; j<15; j++)
board[i][j] = '-';
}

void printBoard()
{
int i,j;
int rowColNumbers[15] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4};

printf("%s", " ");  //Apply one space to align numbers correctly

for(i=0; i<15; i++)
printf("%d", rowColNumbers[i]);

for(i=0; i<15; i++)
{
printf("%s%d", "\n", rowColNumbers[i]);
for(j =0; j<15; j++)
printf("%c", board[i][j]);
}
}```

2. None of those elements exist; your array's last element is [13][13].

3. ahh, thank you. It was throwing me off that the first time the board printed fully but not after the first turn. Thanks again.

4. Macros becomes very handy on these sutitations. You define a macro to the size of the array and use the macros everywhere in your code, while refering the row and columns. Thats would make it much easier for you.

Code:
```#define ROW       15
#define CCOLUMN   15

int array[ROW][COLUMS];```
~ssharish