C++ Multidimensional Arrays – 2D Arrays

In this Tutorial we will learn an understand the concept and working of multidimensional arrays in C++. We will be specifically concentrating of 2-D arrays in C++ for simplicity. If you don’t know the concept of Arrays in C++ you can check it here.

Multidimensional arrays in C++ can be considered as Array of Arrays, which simple means one array inside another.
General Declaration Syntax:

Here’s an Example of a 3-D Array declaration:

Two-Dimensional Arrays

2-D Arrays in C++ are the Simplest form of multidimensional arrays. A 2 dimensional array can be visualized as a table with rows and columns. Thus a matrix like structure can be formed.

c++ multidimensional arrays

Diagram of 2-D Array

Every element in array a is identified by an element name of the form Arr[ i ][ j ]  where Arr is the name of the array, and i and j are the subscripts that uniquely identify each element in Arr.

Declaring a 2-d Array
Initializing 2-D Arrays

There are 2 ways to initialize multidimensional 2-D arrays. They are as follows:
Method 1:

Method 2:

To Initialize on element at a time you can do as follows:

Accessing 2-D Array Elements

An element in the 2-D array can be accessed by using the unique row and column identifiers as follows:

To access all the values inside the 2-D array we can create a nested for loop structure which has a for loop inside a bigger for loop. the outer loop with iterate through the rows which the inner loop will iterate through the columns. Check out the program example (below) to understand its working.

2-D Arrays in C++ Example Program

Run Online

Output
Enter Value of 2-D array A:
1 2 3 4
Values of 2-D array A:
1 2
3 4
Watch it on YouTube

2 thoughts on “C++ Multidimensional Arrays – 2D Arrays

  • May 21, 2018 at 2:44 am
    Permalink

    #include
    using namespace std;
    int main()
    {

    int a[2][2],b[2][2],addition[2][2],subraction[2][2];

    cout<<"enter value of 2-d array a :"<<endl;
    for(int i=0;i<2;i++);
    {
    for(int j=0;j>a[i][j];
    }
    }

    cout<<"enter value of 2-d array b :"<<endl;
    for(int i=0;i<2;i++);
    {
    for(int j=0;j>b[i][j];
    }
    }
    for(int i=0;i<2;i++);
    {
    for(int j=0;j<2;j++)
    {
    addition[i][j]=a[i][j]+b[i][j];
    }
    }
    for(int i=0;i<2;i++);
    {
    for(int j=0;j<2;j++)
    {
    subraction[i][j]=a[i][j]+b[i][j];
    }
    }
    cout<<"value of addition array:"<<endl;
    for(int i=0;i<2;i++);
    {
    for(int j=0;j<2;j++)
    {
    cout<<addition[i][j];
    }
    }

    cout<<"value of subraction array:"<<endl;
    for(int i=0;i<2;i++);
    {
    for(int j=0;j<2;j++)
    {
    cout<<subraction[i][j];
    }
    }

    return 0;
    }

    i am getting error this could you please tell me why it happened
    In function 'int main()':
    14:16: error: 'i' was not declared in this scope
    23:16: error: 'i' was not declared in this scope
    30:18: error: 'i' was not declared in this scope
    37:20: error: 'i' was not declared in this scope
    45:24: error: 'i' was not declared in this scope
    54:26: error: 'i' was not declared in this scope

    Reply
    • May 21, 2018 at 12:55 pm
      Permalink

      Use different variables names in every for loop. That might resolve the issue ­čÖé

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *