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:

type name[size1][size2]...[sizeN];

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

int threeD[5][10][4];
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
type arrayName [ x ][ y ];
Initializing 2-D Arrays

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

int Arr[3][3] = {  
   {0, 1, 2} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10}   /*  initializers for row indexed by 2 */
};

Method 2:

int Arr[3][3] = {0,1,2,4,5,6,8,9,10};

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

int Arr[3][3];
// initialize the first row and second column value to 50
Arr[0][1] = 50;
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:

// access element at 1st row and 2nd column of array Arr 
// and store it in a new variable x
int x = Arr[0][1];

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

#include<iostream>
using namespace std;
int main()
{
	int A[2][2];
	// take input from user using 2 for loops
	cout<<"Enter values of 2-D array A: "<<endl;
	for(int i=0;i<2;i++)
	{
		for(int j = 0; j<2;j++)
		{
			cin>>A[i][j];
		}
	}
	// print 2-d Array values as output on console
	cout<<"Values of 2-D array A: "<<endl;
	for(int i=0;i<2;i++)
	{
		for(int j = 0; j<2;j++)
		{
			cout<<A[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0;
}
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 *