Hey guys, what is up. In this particular post, we will be discussing

what is a two dimensional array. Also abouthow we can declareandinitialize a two-dimensional array in c++.

Updated on 1/10/2021

KEY POINTS

*What is a two dimensional array?*

*What is a two dimensional array?*

A two-dimensional **array **is one of the simplest forms of a multi-dimensional array. The two-dimensional is a collection of arrays itself. The name 2-D represents that it is a collection of rows and columns both. Moreover, in the singular form, it is known as matrix and in the plural form, it is known as matrices. However, it is easy to access and hold a bulk of data at one time which can be accessed whenever needed.

__Declaration of two dimensional array in C++:__

__Declaration of two dimensional array in C++:__

The syntax of the 2-D array is written below:

*Data_type Name_of_array[Number of rows][Number of columns]*

** Example**:

*int hello[10][20];*Here 10 is the number of rows and 20 is the number of columns in an array. Here the number of elements that can be stored in array hello is the product of rows and columns. 10×20=200. Therefore, the highest number of data elements that can be stored in this array is 200. Also, the 2-D array is like a table in which the number of rows present in the array ranges from 0 to the number of rows – 1. Also, the column ranges from 0 to the number of columns – 1. The tabular form of a 2-D array which is * X[3][2]* is

__Initialization of 2-D Array:__

__Initialization of 2-D Array:__

As we have seen a one-dimensional array, there is no need to specify the size of an array if the declaration and initialization of an array at the same time. Moreover, this method will not work in two-dimensional arrays. In this, we have a choice of giving the number of rows but the number of rows is compulsory. A two-dimensional array can be initialized in the following way:-

__FIRST METHOD:__

*int x[4][3] = {6, 8, 10, 12, 13, 5, 16, 27, 38 , 22, 62, 100}*

Therefore, the given array has 4 rows and 3 columns. The elements initialized in the braces from the starting point are stored in the table/ array from left to right. The elements in the array will be stored in the order, in which the first 3 elements are filled in the first row from the left side. The next 3 elements in the second row, and so on.

__SECOND METHOD:__

*int x[4][3] = { {6, 8, 10}, {12, 13, 5}, {16, 27, 38}, {22, 62, 100} }*

The second method is consider as a better method. This method of declaration makes use of braces. The inner brackets represent the elements in the first row. In the given example four braces represent that there are four rows in the array.

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int A[2][2], i, j;

for(i=0; i<2; i++)

{

for(j=0; j<2; j++)

{

cin>>A[ i ][ j ];

}

}

cout<<endl<<“ARRAY ELEMENTS:”<<endl;

for(i=0; i<2; i++)

{

for(j=0; j<2; j++)

{

cout<<A[ i ][ j ]<<“\t”;

}

cout<<endl;

}

}

In the upcoming post, you will be getting information related to the

and also aboutdifference between CPU and GPUhow to calculate sum of 2 arrays.If you haven’t read abouthow to merge sorted and unsorted arraysthen go and check this out.You can also checkgeeks for geeks. Also, stay tuned for the upcoming posts.

## 2 Comments

## Mohit kashyap · June 8, 2021 at 4:03 pm

Nice and informative content.

## Naman Kumar · June 9, 2021 at 1:45 pm

Indeed. Very useful