Two dimensional arrays in C++ programming language

 


Two dimensional or 2d arrays in C++ programming language

Last updated on : October 24, 2026

In todays blog post we are going to cover a very important concept with respect to arrays in C++ programming language. This concept is about two-dimensional arrays in C++ program. Two-dimensional arrays are also referred as 2d arrays and they are basically array of arrays. We can simply store data in two-dimensional arrays or 2d arrays in a grid like structure which consists of rows and columns. And keep this one important point in your mind that in 2d arrays, again the starting index for both rows and columns is 0 (zero). It means that if you want to access the first element of 2d array you have to put 0 in both rows and columns square brackets like this,

string words[2][2] = { { "hello", "World" },{ "good", "bad" } };
cout << words[0][0] << endl;

In this way we can access the very first element or value of two-dimensional array. We also refer two-dimensional or 2d arrays as multi-dimensional arrays in C++ programming language. Before diving deep into the concept of two dimensional arrays let us look at the basic syntax and working of 2d arrays in C++ programming language. Before moving to the syntax part let us keep some important points in our mind. These 2d arrays or multi-dimensional arrays are also knowns as rectangular array in C++. And remember that we can stroe data in them in tabular format that is (rows and columns), which is also known as matrix. And these multi-dimensional array are a collection of homogeneous items, and each item can be accessed by using multiple indices.



Basic syntax and working of two-dimensional arrays in C++ programming language


Different syntax examples

DataType ArrayName[RowSize][ColumnSize];

int numbers[2][2];
string words[2][2]; int numbers[2][2] = { { 1,2 },{ 3,4 } };
int numbers[2][2] = { { 1,2 },
		      { 3,4 } };

string
words[2][2] = { { "hello", "world" },{ "good", "bad" } }; string words[2][2] = { { "hello", "world" }, { "good", "bad" } };


Explanation of syntax

The syntax of declaring two-dimensional arrays in C++ programming is almost similar to how we can declare and initialize one-dimensional array in C++ programming language. In the above syntax as shown, first we have to declare a data type for array variable, then we assign the name to the array variable, followed by a set of two opening and closing square brackets like this,

int numbers[2][2];

The first opening and closing square brackets determines the row size or number of rows and the second opening and closing square brackets determines the column size or the number of columns. 

DataType ArrayName[RowSize][ColumnSize];

In the next six lines we have mentioned different ways to declare and initialize 2d arrays in C++ programming language format.

In one-dimensional array we have one set of opening and closing square bracket like this,

int number[3];

Where as in 2d arrays we have two sets of opening and closing square brackets. Now in order to assign values to two-dimensional arrays we have to give the index numbers to both of the square brackets, in which first determines number of rows and second one determines number of columns like this, 

string words[2][2] = { { "hello", "World" },{ "good", "bad" } };

And when we want to print a specific number to the console screen using 2d arrays we can simply write cout statement like this, 

cout << words[0][0] << endl;

in order to show the value to the console screen.


Simple coding examples of two-dimensional or 2d arrays in C++ programming language

First C++ coding example

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	string words[2][2] = { { "hello", "World" },{ "good", "bad" } };
cout << words[0][0] << endl; cout << words[0][1] << endl; cout << words[1][0] << endl; cout << words[1][1] << endl; _getch(); }




Explanation of code 1

In the first C++ coding example we have declared and initialized a two-dimensional or 2d array of type string and named it as words and we gave the number of rows and number of columns as 2 like this,

string words[2][2] = { { "hello", "World" },{ "good", "bad" } };

And after that we initialized the 2d array with four string values. Then we simply printed the four string values manually to the console screen using the specific index numbers for both rows and columns like this,

cout << words[0][0] << endl;
cout << words[0][1] << endl;
cout << words[1][0] << endl;
cout << words[1][1] << endl;

like in the first cout statement we gave 0 value to both the indexs which refers to the value hello this simply means that the value hello is placed at 0 number row and 0 number column like this,

cout << words[0][0] << endl;    // refers to hello value.

And in the same way we have displayed the rest of the values to console screen using their specific indexes.

You can look at the above image to gain some understanding of indexes and array values in a very simple and easy way.


Second C++ coding example

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	string words[2][2] = { { "hello", "world" },
			       { "good", "bad" } };
for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { cout << "words[" << i << "][" << j << "] = " << words[i][j] << endl; } } _getch(); }


Explanation of code 2

In second C++ coding example we have the same string 2d array with same size and initialized it with same string values as in the above example like this,

string words[2][2] = { { "hello", "world" },
		       { "good", "bad" } };

The only difference in this example is that we have used nested for loop for traversing or displaying the values of 2d array to the console screen like this,

for (int i = 0; i < 2; i++)
{
	for (int j = 0; j < 2; j++)
	{
		cout << "words[" << i << "][" << j << "] = " << words[i][j] << endl;
	}
}

Just keep one thing in mind that the outer for loop variable i will control the row number index of 2d array and the inner for loop variable j will control the column number index of 2d array.


Third C++ coding example

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	int numbers[2][2] = { { 1,2 },
			      { 3,4 } };
for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { cout << "numbers[" << i << "][" << j << "] = " << numbers[i][j] << endl; } } _getch(); }


Explanation of code 3

In the third C++ coding example we have declared and initialized 2d int array with same size for number of rows and number of columns as 2, and named 2d array as numbers like this,

int numbers[2][2] = { { 1,2 },
		      { 3,4 } };

And again we used nested for loop to display the 2d int array values to the console screen like this,

for (int i = 0; i < 2; i++)
{
	for (int j = 0; j < 2; j++)
	{
		cout << "numbers[" << i << "][" << j << "] = " << numbers[i][j] << endl;
	}
}

Just keep one important point in mind that in 2nd and 3rd C++ coding examples we have simply shown the initialized value in tabular format so that you can get better understanding of rows and columns structure like this,

{ { "hello", "world" },    // second coding example.
  { "good", "bad" } };


{ { 1,2 },                 // third coding example.
  { 3,4 } };


And the rest of the program logic is same.

Congratulations! like always we have successfully implemented the two-dimensional or 2d arrays in C++ programming language.


Comments

Popular posts from this blog

How to Download and Install Microsoft visual studio community 2022 for free

For Loop In C++ Programming Language

Hello World Program In C Programming Language