Manik Sharma Manik Sharma - 4 months ago 13
C Question

Print two-dimensional array with a function

void printBoard(int board[8][8])
{
int i,j;
for(i = 0; i<8; i++)
{
for(j = 0; j<8; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
}
}

int main(){
int i, j;
int board[8][8];

for(i = 0; i<8; i++)
{
for(j = 0; j<8; j++)
{
scanf("%d", &board[i][j]);
}
}

printBoard(board);


Why does the following void function not print the two dimensional array? It just continues asking for input. I tried it without the function as well, and it still doesn't work.

We were meant to use this as part of our homework assignment. Can someone please explain how to get around this?

Answer

Your main is looping through scanf which is a function that asks for user input 64 times. You need to input 64 values before the printBoard() statement is reached.