December 2, 2023 # What is a two dimensional array

Hey guys, what is up. In this particular post, we will be discussing what is a two dimensional array. Also about how we can declare and initialize a two-dimensional array in c++.

Updated on 1/10/2021

KEY POINTS

## 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++:

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

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

Example: int hello;

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 is

## 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 = {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 = { {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, 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 difference between CPU and GPU and also about how to calculate sum of 2 arrays. If you haven’t read about how to merge sorted and unsorted arrays then go and check this out. You can also check geeks for geeks. Also, stay tuned for the upcoming posts. #### Sneha Shukla

View all posts by Sneha Shukla →

## 2 thoughts on “What is a two dimensional array”

1. Mohit kashyap says:

Nice and informative content.

1. Naman Kumar says:

Indeed. Very useful