2D Array with Example C Program

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Explain Two-Dimensional (2D) Array with an Example Program

Explanation Video:

Custom Image

Explanation:

 

A 2D array is an array of arrays, meaning it has rows and columns like a table or matrix. It is useful for storing tabular data, such as marksheets, matrices, or game boards.

Declaration and Initialization:

  • Declaration specifies the number of rows and columns.
  • Initialization assigns values to the array elements.

Syntax:

data_type array_name[rows][columns];

data_type array_name[rows][columns] = { {value1, value2}, {value3, value4} };

Key Points:

Elements are stored row-wise.
 Accessing elements requires two indices (row and column).
Can be used for matrices, seating arrangements, or game boards.

Source Code:
#include <stdio.h>

int main() {
    int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };  // 2 rows, 3 columns

    // Printing 2D array elements
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n"); // Newline after each row
    }

    return 0;
}
Output:
1 2 3
4 5 6
Share this Article & Support Us:
Status
printf('Loading...');