Two Dimensional Array in Data Structure | Matrix, Traversal & Operations

Two-Dimensional Array in Data Structure

A two-dimensional array stores elements using two indexes: one identifies the row and the other identifies the column. This organization is useful when data naturally has a grid or matrix structure.

In C, a two-dimensional array is declared with two dimensions. For example, int matrix[3][4] represents three rows and four columns. An element is accessed using both indexes, such as matrix[1][2].

Understanding Rows and Columns

             Column
              0     1     2
           +-----+-----+-----+
Row 0      |  10 |  20 |  30 |
           +-----+-----+-----+
Row 1      |  40 |  50 |  60 |
           +-----+-----+-----+

The value 50 is located at row 1, column 1, so it can be accessed as matrix[1][1].


Declaration

The general C syntax is:

data_type array_name[rows][columns];

Example:

int matrix[2][3];

This creates space for 2 × 3 = 6 integer elements. The valid row indexes are 0 and 1, while the valid column indexes are 0, 1 and 2.


Initialization

Values can be supplied row by row when the array is initialized.

int matrix[2][3] = {
    {10, 20, 30},
    {40, 50, 60}
};

The first nested initializer represents row 0 and the second represents row 1.


Accessing an Element

Two indexes are required to access an element: one for the row and one for the column.

printf("%d", matrix[1][2]);

The statement above prints 60 because row 1, column 2 contains 60.


Traversing a Two-Dimensional Array

A nested loop is commonly used because the outer loop can move through rows while the inner loop moves through columns.

#include <stdio.h>

int main()
{
    int matrix[2][3] = {
        {10, 20, 30},
        {40, 50, 60}
    };

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", matrix[i][j]);
        }

        printf("\n");
    }

    return 0;
}

Output:

10 20 30
40 50 60

If a matrix contains r rows and c columns, visiting every element takes O(r × c) time.


Row-Wise Processing

A two-dimensional array is often processed one row at a time. For example, the following program calculates the total of each row.

#include <stdio.h>

int main()
{
    int matrix[3][3] = {
        {2, 4, 6},
        {1, 3, 5},
        {7, 8, 9}
    };

    for (int i = 0; i < 3; i++)
    {
        int sum = 0;

        for (int j = 0; j < 3; j++)
        {
            sum += matrix[i][j];
        }

        printf("Row %d total = %d\n", i + 1, sum);
    }

    return 0;
}

This pattern is useful when each row represents an independent group of values, such as the marks of one category or measurements collected for one item.


Column-Wise Processing

The loop order can also be used to process columns. The outer loop selects a column and the inner loop moves through its rows.

for (int j = 0; j < 3; j++)
{
    int sum = 0;

    for (int i = 0; i < 3; i++)
    {
        sum += matrix[i][j];
    }

    printf("Column %d total = %d\n", j + 1, sum);
}

This distinction between row-wise and column-wise processing is one of the important ideas that separates a 2D array from a simple 1D sequence.


Matrix Addition

Two matrices can be added when they have the same number of rows and columns. Corresponding elements are added to produce the result.

A =  1  2
     3  4

B =  5  6
     7  8

A + B =  6  8
        10 12

For an r × c matrix, matrix addition requires O(r × c) element operations.


Matrix Transpose

The transpose of a matrix exchanges its rows and columns. Therefore, an element at A[i][j] becomes T[j][i].

Original:

1 2 3
4 5 6

Transpose:

1 4
2 5
3 6

Transpose is useful in matrix algorithms and also provides a practical example of changing the order in which two-dimensional data is represented.


Searching in a 2D Array

If no additional structure is known about the matrix, a simple search checks each element using nested loops.

int target = 50;
int found = 0;

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        if (matrix[i][j] == target)
        {
            printf("Found at row %d, column %d\n", i, j);
            found = 1;
            break;
        }
    }

    if (found)
        break;
}

For an r × c matrix, an unrestricted linear search has O(r × c) worst-case time. Specially sorted matrices can support more advanced search strategies.


Memory Layout of a 2D Array in C

A C two-dimensional array is stored in a well-defined contiguous layout. For the usual C representation, the rightmost subscript varies fastest, which is commonly described as row-major order.

For example:

int a[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

The elements are laid out in row order conceptually as:

1  2  3  4  5  6

Understanding this layout helps explain why nested loops are normally written with rows as the outer dimension and columns as the inner dimension when traversing a C matrix.


Common Operations and Complexity

Operation Typical Time Notes
Access one element O(1) Both indexes are known.
Traverse complete matrix O(r × c) Every element is visited.
Unrestricted search O(r × c) Every position may need to be checked.
Transpose O(r × c) Elements are copied or rearranged.
Matrix addition O(r × c) Requires one operation for each corresponding pair.

Applications of Two-Dimensional Arrays


Advantages


Limitations


Common Mistakes


1D Array vs 2D Array

Feature 1D Array 2D Array
Indexes One Two
Structure Linear sequence Rows and columns
Example arr[4] matrix[2][3]
Typical traversal One loop Nested loops
Common use Lists and sequences Matrices, grids and tables

Frequently Asked Questions

What is a two-dimensional array?

A two-dimensional array is an indexed structure in which each element is identified using a row and a column index.

How do you access an element?

Use two indexes. For example, matrix[1][2] accesses row 1 and column 2.

Why are nested loops used?

One loop can select rows while another selects columns, allowing every position to be visited systematically.

What is row-major order?

In C, a multidimensional array is laid out so that the rightmost subscript changes fastest. For a normal 2D array this corresponds to row-major order.

What is the complexity of traversing a matrix?

For r rows and c columns, visiting every element takes O(r × c) time.


Conclusion

A two-dimensional array extends the indexed-array idea into two coordinates. Its row-and-column organization makes it a natural choice for matrices, tables, grids and other structured data.

The key concepts to master are row/column indexing, nested-loop traversal, matrix operations, memory layout and correct boundary handling. These ideas are widely used in algorithms, programming problems and numerical applications.

← Previous: One-Dimensional Array Next: Linked List →
Home Visit Our YouTube Channel