how to print a 2d array in c code example

Example 1: matrix c declaration

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Example 2: double array in c

int disp[2][4] = {
    {10, 11, 12, 13},
    {14, 15, 16, 17}
};

Example 3: C print 2D array

#include <stdio.h>

#define MAX 10

int main()
{
    char grid[MAX][MAX];
    int i,j,row,col;

    printf("Please enter your grid size: ");
    scanf("%d %d", &row, &col);


    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            grid[i][j] = '.';
            printf("%c ", grid[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Example 4: how to print a 2d array in c++

for (int i = 0; i < m; i++) 
{ 
   for (int j = 0; j < n; j++) 
   { 
      cout << arr[i][j] << " "; 
   } 
     
   // Newline for new row 
   cout << endl; 
}

Tags:

Cpp Example