# Thread: Printing a 2D array

1. ## Printing a 2D array

I want to display a 2D array, but my output is:
22 34 5 648 798 2477 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

What is wrong with my code since only the last column is displayed ?
Code:
```    int num_array[6][5] =
{
( 82,3,36,8,22 ) ,
( 6,2,88,9,34 ) ,
( 65,26,42,1,5 ) ,
( 46,1521,51,7,648 ) ,
( 14,39,5,792,798 ) ,
( 47,11,348,547,2477 )
};

int nNumRows = 6;
int nNumCols = 5;

for (int nRow = 0; nRow < nNumRows; nRow++)
for (int nCol = 0; nCol < nNumCols; nCol++)
cout << num_array[nRow][nCol] << " ";```

2. You've used parentheses and not braces in your array initialiser.

3. Why did it compile? In using parentheses, you've inadvertently used the comma operator.
Code:
`( 82,3,36,8,22 )`
That is an expression. The comma operator evaluates its left expression, then its right, and the value of the expression has the value of the right-hand side. When you combine multiple commas, you get something like
Code:
`( (((82,3),36),8),22 )`
which evaluates to 22 in the end.

The comma operator is one of the least-used operators. Perhaps the most useful place for it is in for loops:
Code:
`for(x = 0, y = 0; x < MAX; x ++, y ++)`
In array initializers, any elements not specified are set to zero. That's why the remaining elements in your array became zero.

One other bit of knowledge is required to understand what happened: you can leave out nested curly braces. For example, this
Code:
`int array[2][2] = {{1, 2}, {3, 4}};`
can be written as
Code:
`int array[2][2] = {1, 2, 3, 4};`
Many people consider this bad programming practice, and your compiler will probably warn you about it if you turn up the warning level. But it's part of C, at least C89.

Anyway, just some useless information. As Salem said, you want to use
Code:
`{}`