CS Pathfinder Logo CS Pathfinder

Unit 1 · Foundation of DSA

Array (2D)

Learn two-dimensional arrays in DSA with matrix representation, row-column indexing, traversal, operations, and C examples.

Introduction

A two-dimensional array stores data in rows and columns, just like a table or matrix.

2D arrays are useful for matrices, grids, images, game boards, and many graph representation techniques.

Each element is accessed using two indexes: row index and column index.

        Column
        0   1   2
Row 0:  1   2   3
Row 1:  4   5   6
Row 2:  7   8   9

Where 2D Arrays Are Used

Matrices

Mathematical matrix operations use 2D arrays.

Tables

Rows and columns can store structured records.

Grids

Games, maps, and boards often use grid storage.

Images

Pixels can be represented using row and column positions.

Table of Contents

What is a 2D Array?

A 2D array is an array of arrays. It stores elements in tabular form.

In C, a 2D array declaration such as int matrix[3][3] creates 3 rows and 3 columns.

Access syntax: matrix[row][column]

Declaration and Initialization

A 2D array needs row size and column size. Values are usually written row by row.

  • int matrix[2][3]; declares a matrix with 2 rows and 3 columns.
  • int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; initializes values.
  • matrix[1][2] accesses the element in second row and third column.

Traversing a 2D Array

2D arrays are usually traversed using nested loops. The outer loop handles rows, and the inner loop handles columns.

  • Row-major traversal visits one complete row at a time.
  • Column-major traversal visits one complete column at a time.
  • C stores 2D arrays in row-major order.

Applications of 2D Arrays

2D arrays appear whenever data has two-dimensional structure.

  • Matrix addition and multiplication.
  • Sparse matrix representation.
  • Chess board or tic-tac-toe board.
  • Graph adjacency matrix.
  • Image pixel processing.

2D Array Operation Complexity

Operation Time Complexity Explanation
Access matrix[i][j] O(1) Direct row-column address calculation
Traverse all elements O(rows * columns) Every cell is visited
Search unsorted matrix O(rows * columns) May scan all cells
Print diagonal of square matrix O(n) Only n diagonal elements

C Program Example

This program prints a 3 by 3 matrix using nested loops.

#include <stdio.h>

int main() {
    int matrix[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("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output: 1 2 3 4 5 6 7 8 9

Key Points

  • A 2D array stores elements in rows and columns.
  • Each element is accessed using row and column indexes.
  • Nested loops are used for traversal.
  • 2D arrays are useful for matrices, grids, images, and adjacency matrices.

Interview Tip

When solving matrix problems, clearly identify row count, column count, and boundary conditions before coding.

Common Beginner Mistakes

  • Mixing row index and column index.
  • Using wrong loop limits.
  • Forgetting that C indexes start from 0.
  • Assuming every matrix is square.

Quick Revision

  • Syntax: array[row][column].
  • Traversal usually needs nested loops.
  • Total elements = rows * columns.
  • C stores 2D arrays row by row.

Summary

A two-dimensional array is a matrix-like structure that stores data in rows and columns. It is essential for matrix operations, grid-based problems, and many real-world data representations.

Data Structures & Algorithms Handwritten Notes

Master DSA with 142 Pages of Easy Handwritten Notes – Perfect for Interviews, Placements, GATE & Exams.