🟡 Intermediate  ·  Lesson 21

2D Arrays & Matrix

2D Arrays और Matrix

What is a 2D Array?

A 2D array is an array of arrays — essentially a matrix with rows and columns. It is used to store tabular data such as marks of students in multiple subjects, matrices in mathematics, or any grid-based data.

Think of it as a table with rows and columns. A 2D array with 3 rows and 4 columns has 3 × 4 = 12 elements.

Concept
// 2D array = matrix
//        col0  col1  col2  col3
// row0:   10    20    30    40
// row1:   50    60    70    80
// row2:   90   100   110   120
// Accessed as: arr[row][col]

Declaration and Initialization

C Language
// Syntax: type name[ROWS][COLS];
int matrix[3][4];   // 3 rows, 4 cols — garbage values

// Initialize with values
int a[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

// Initialize all to zero
int zeros[4][4] = {0};

Accessing Elements & Traversal

C Language – Print 2D Array
#include <stdio.h>
int main() {
    int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    printf("Matrix:\n");
    for(int i=0; i<3; i++) {
        for(int j=0; j<3; j++)
            printf("%4d", a[i][j]);
        printf("\n");
    }
    return 0;
}
Matrix: 1 2 3 4 5 6 7 8 9

Matrix Operations

Matrix Addition

C Language – Matrix Addition
#include <stdio.h>
#define R 2
#define C 2
int main() {
    int a[R][C] = {{1,2},{3,4}};
    int b[R][C] = {{5,6},{7,8}};
    int sum[R][C];
    for(int i=0; i<R; i++)
        for(int j=0; j<C; j++)
            sum[i][j] = a[i][j] + b[i][j];
    printf("Sum:\n");
    for(int i=0; i<R; i++) {
        for(int j=0; j<C; j++) printf("%4d", sum[i][j]);
        printf("\n");
    }
    return 0;
}
Sum: 6 8 10 12

Matrix Multiplication

C Language – Matrix Multiplication
#include <stdio.h>
int main() {
    int a[2][2] = {{1,2},{3,4}};
    int b[2][2] = {{5,6},{7,8}};
    int c[2][2] = {0};
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
                c[i][j] += a[i][k] * b[k][j];
    printf("Product:\n%d %d\n%d %d\n",c[0][0],c[0][1],c[1][0],c[1][1]);
    return 0;
}
Product: 19 22 43 50

Transpose of a Matrix

C Language – Transpose
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
printf("Transpose:\n");
for(int i=0;i<3;i++) {
    for(int j=0;j<3;j++) printf("%4d", a[j][i]); // swap i,j
    printf("\n");
}
Transpose: 1 4 7 2 5 8 3 6 9

Student Marks Example

C Language – Student Marks
#include <stdio.h>
int main() {
    int marks[3][4] = {
        {85, 90, 78, 92},   // Student 1
        {70, 65, 88, 75},   // Student 2
        {95, 88, 91, 87},   // Student 3
    };
    for(int i=0; i<3; i++) {
        int sum=0;
        for(int j=0; j<4; j++) sum += marks[i][j];
        printf("Student %d: Total=%d, Avg=%.1f\n", i+1, sum, (float)sum/4);
    }
    return 0;
}
Student 1: Total=345, Avg=86.2 Student 2: Total=298, Avg=74.5 Student 3: Total=361, Avg=90.2

Summary

  • 2D array = matrix with rows and columns: type name[ROWS][COLS]
  • Access element: arr[row][col] — both indices start at 0
  • Traverse with nested for loops: outer = rows, inner = columns
  • Common uses: student marks, matrix operations, game boards, image pixels
  • Size in memory: ROWS × COLS × sizeof(type) bytes

2D Array क्या है?

2D array arrays का array है — essentially एक matrix जिसमें rows और columns होते हैं। इसे tabular data जैसे students के multiple subjects में marks, mathematics में matrices, या किसी भी grid-based data store करने के लिए use किया जाता है।

Concept
// 2D array = matrix (rows और columns)
//        col0  col1  col2
// row0:   10    20    30
// row1:   40    50    60
// Access: arr[row][col]   — दोनों 0 से शुरू

Declaration और Initialization

C Language
int matrix[3][3];   // Declare only — garbage values
int a[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};  // Initialize with values
int zeros[4][4] = {0}; // सब 0

Matrix Operations

C Language – Matrix Print
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for(int i=0; i<3; i++) {
    for(int j=0; j<3; j++)
        printf("%4d", a[i][j]);
    printf("\n");
}
1 2 3 4 5 6 7 8 9
💡 Student Marks Example

marks[3][4] — 3 students, 4 subjects। marks[0][2] = पहले student का तीसरा subject।

सारांश

  • 2D array = rows और columns वाला matrix: type name[ROWS][COLS]
  • Element access: arr[row][col] — दोनों indices 0 से शुरू
  • Traverse: outer loop = rows, inner loop = columns
  • Uses: student marks, matrix calculations, game boards, image processing
← Back to C Tutorial