DEV Community

Cover image for 2D Array in C++
Samiun Black
Samiun Black

Posted on • Edited on

2D Array in C++

Introduction

A 2D array is nothing but a giant array of 1D arrays. In a normal 1D array we can see that the array contains data like int, char, double, string, etc. In the same way, a 2D array contains 1D arrays instead of data. So we can call it an array of arrays.

Declaration and Initialization

int row = 3;
int column = 4
int arr[row][column] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
};

Enter fullscreen mode Exit fullscreen mode

For declaring a 2D array we write the array name followed by its row number and column number. So in the example, the array arr contains 3 rows and 4 columns. For initializing we write the rows as a 1D array and fill up the array with elements equal to the column number.

We can initialize a 2D array like this too-

int arr[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

Enter fullscreen mode Exit fullscreen mode

This array too has 3 rows and 4 columns but it looks a little less organized. So the first approach is preferred.

Traversing 2D Array

For traversing a 2D array we use a nested for loop. We can traverse a 2D Array in two ways-

  1. Row Major Traversing
  2. Column Major Traversing

Row Major Traversing

https://static-assets.codecademy.com/Paths/ap-computer-science/TwoDArrays/row_major.png

This is the common way of traversing a 2D array. We can do it with a nested for loop like this-

int row = 3, col = 4;
int arr[row][col] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
};

for(int i = 0; i < row; i++)
{
    for(int j = 0; j < col; j++)
    {
        cout << arr[i][j] << " ";
        //output: 1 2 3 4 5 6 7 8 9 10 11 12
    }
}

Enter fullscreen mode Exit fullscreen mode

In the row-major traversing the outer loop iterates over the rows while the inner loop iterates over the column. As a result, when we access arr[i][j] in each iteration we get row by row elements.

Column Major Traversing

https://static-assets.codecademy.com/Paths/ap-computer-science/TwoDArrays/column_major.png

This is a tricky way of traversing and in most places, it's not taught. We can do column-major traversing with a nested for loop like this-

int row = 3, col = 4;
int arr[row][col] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
};

for(int i = 0; i < col; i++)
{
    for(int j = 0; j < row; j++)
    {
        cout << arr[j][i] << " ";
        //output: 1 5 9 2 6 10 3 7 11 4 8 12
    }
}

Enter fullscreen mode Exit fullscreen mode

In the column-major traversing the outer loop iterates over the column while the inner loop iterates over the rows. Point to be noted- here we are visiting arr[j][i] instead of arr[i][j] which means we are visiting elements of each column.

Pointer to a 2D Array

If we can have a pointer to an integer, a pointer to a float, a pointer to a char, then can we can also have a pointer for 2D array. The following program shows how to build and use it.

int row = 3, col = 4;
int arr[row][col] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
};

int *p;

for (int i = 0 ; i < 3 ; i++)
{
    p = &arr[i][0];
    cout << "Row " << i << ":";

    for (int j = 0; j < 4; j++)
        cout << "\t" << *(p+j);

  cout << endl;
}

Enter fullscreen mode Exit fullscreen mode

In the above code, we try to print a 2D array using pointers. At first we initialize a 2D array, arr[row][col] and a pointer (*p)[4], where p is a pointer which stores the address of the first element of the array. We use a for loop to traverse over these 3 elements of the 2D array arr. For each iteration, we assign p with the address of arr[i][0], which is the address of first element of every row. Further, the inner for loop prints out the individual elements of the array arr[i] using the pointer p. Here, *(p + j) gives us the address of the individual element arr[i][j], so using *(p+j) we can access the corresponding value.

Passing a 2D array in a Function

We can pass 2D array into as a function parameter just like any other data. In the code below, we pass the array arr, to a function printArray() which prints out the passed 2D array.

#include<iostream>
using namespace std;

void printArray(int  a[][4], int row, int col)
{
    for(int i = 0 ; i < row; i++)
    {
         for(int j = 0; j < col; j++)
            cout << "\t" << a[i][j];
         cout << "\n";
    }
      cout << "\n";
}

int main()
{
  int  arr[3][4] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21} ;

  printArray(arr, 3, 4);
  return 0;
}

Enter fullscreen mode Exit fullscreen mode

In the function printArray(), the declaration of a looks like this: int a[][4]. Here a is a pointer to an array of 4 integers. Inside the function we can use the array just as we used in other places.

Conclusion

In this article we discussed 2D array C++, how we can traverse it, how to have a pointer of it and lastly how to pass it to a function. If you have any confusion or question you can knock me up at samiunblack@gmail.comHappy Coding

Top comments (0)