🟡 Intermediate  ·  Lesson 21

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

Frequently Asked Questions

What is a two-dimensional array in C?
A two-dimensional array stores data in a grid of rows and columns, declared with two sizes such as int matrix[3][4]; for 3 rows and 4 columns. You access each element with two indexes, like matrix[i][j], making it ideal for tables and matrices.
How do you declare a 2D array in C?
You give the type, name and two sizes in brackets, such as int a[3][3];. You can initialise it with nested braces, for example int a[2][2] = {{1, 2}, {3, 4}};, where each inner set of braces is one row.
How is a 2D array stored in memory in C?
C stores a 2D array in row-major order, meaning all of row 0 is laid out first, then all of row 1, and so on, in one continuous block of memory. This is why looping row by row is usually more cache-friendly than column by column.
How do you access elements of a 2D array?
You use two indexes: the first for the row and the second for the column, such as a[i][j]. Nested loops — an outer loop over rows and an inner loop over columns — are the usual way to visit every element.
What are 2D arrays used for in C?
Two-dimensional arrays are used for anything grid-shaped: matrices, game boards, tables of data, images as pixel grids, and seating charts. Whenever data naturally has rows and columns, a 2D array is a good fit.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.