Understanding 2 Dimensional Arrays in C and C++

Last modified - 02-05-2025

Author - Krishna Shinde


A 2-dimensional array also known as a matrix is one of the concepts of data structure, which allows us to store the data in the tabular form that includes rows and columns, making it ideal to solve problems which has grids, tables, or matrices. In both C and C++, a 2d array is used in complex tasks like image processing or mathematical computations, it is also used in various games.

In this article, we’ll explore 2-dimensional arrays in C and C++ in detail, how they are used in data structures, why we need 2-D arrays, how they are stored in memory, and how to declare, initialize, and manipulate them effectively.

What is a 2 Dimensional Array?

A 2-D array is an array of arrays that stores data in grid form using rows and columns instead of a 1-D array which stores data in a single line.

Syntax code in c and cpp

data_type array_name[row_size][column_size];

Example

int matrix[3][4];  // A 3x4 matrix with 3 rows and 4 columns

The capacity of the array is the product of row size and column size, in this case, it is 3×4 which is 12. So the array numbers are capable of storing 12 elements.

Declaring and Initializing a 2D Array in C

In C programming, 2D arrays are declared as:

int arr[2][3];

You can initialize the array in multiple ways:

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

// or

int arr[2][3] = {1, 2, 3, 4, 5, 6};  // Auto fills row-wise

Accessing elements:

printf("%d", arr[0][1]);  // Outputs 2

Declaring and Initializing a 2D Array in C++

In C++, the syntax for 2D arrays is the same as in C:

int arr[2][3];

C++ also allows using vectors for dynamic 2D arrays:

#include <vector>
using namespace std;

vector<vector<int>> matrix(2, vector<int>(2, 0));

This approach can be used when the size of the array is unknown during compile time.

2D Arrays in Data Structures

In data structures, 2-dimensional arrays are used in various ways:

  • Matrices – Representing mathematical matrices for linear algebra.
  • Graphs – Representing adjacency matrices in graph theory.
  • Grids – Simulating chessboards, game maps, or puzzles.
  • Dynamic Programming (DP) – Storing intermediate results in tabular form.
  • Image Representation – Storing pixel values in image processing.
  • Matrix Addition in C:

    int a[2][2] = {{1, 2}, {3, 4}};
    int b[2][2] = {{5, 6}, {7, 8}};
    int sum[2][2];
    
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            sum[i][j] = a[i][j] + b[i][j];
        }
    }

    Time Complexity : O(1)

    Space Complexity : O(1)

    Traversing a 2D Array

    To traverse a 2D array, we use nested loops:

    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < columns; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    Time Complexity : O(m*n)

    Space Complexity : O(1)

    This approach is the same in both C and C++.

    How 2D Array is Stored in Memory

    2D arrays in C and C++ are stored in linear order, which means the entire first row is stored in memory first, followed by the second row, and so on.

    How 2d array is stored in memory

    Linear Search on a 2D Array

    Linear search is a simple search algorithm where each element is checked one by one until the target element is found or the entire array is traversed.

    #include <iostream>
    using namespace std;
    
    bool linearSearch2D(int arr[][3], int rows, int cols, int key) {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (arr[i][j] == key) {
                    cout << "Element found at position: [" << i << "][" << j << "]
    ";
                    return true;
                }
            }
        }
        cout << "Element not found.
    ";
        return false;
    }
    
    int main() {
        int arr[2][3] = {
            {10, 20, 30},
            {40, 50, 60}
        };
    
        int key = 50;
        linearSearch2D(arr, 2, 3, key);
    
        return 0;
    }

    Time Complexity : O(rows * columns)

    When to use a 2D Array

    We use 2D arrays when we need to store the data in a tabular or grid format, which has rows and columns.

  • To Represent Matrices
  • To Store Tabular Data
  • For Grid-Based Problems
  • In Data Structures
  • Conclusion

    The 2D array is an efficient and important concept of programming, it helps to solve grid-based or matrix-based problems, and it plays a crucial role in data structures, problem-solving, and competitive programming. Visit Algoflame for more programming blogs.