1.
Introduction
2.
Two Dimensional Array
2.1.
Initialization of a two dimensional Array:
2.2.
Accessing Elements in a Two dimensional Array
3.
Three Dimensional Array
3.1.
Initialization of a three-dimensional array:
3.2.
Accessing Elements in a three-Dimensional Array:
4.
FAQs
5.
Key Takeaways:
Last Updated: Mar 27, 2024

# Multi-Dimensional Arrays

Vivek Goswami
Basics of C++
Free guided path
9 chapters
99+ problems

## Introduction

An array is the collection of similar objects stored in a contiguous memory location. The dimension of an array determines the number of arrays present in an array. Multi-dimensional arrays store data in tabular form.

A single-dimensional array has just a single kind of multiple values that can be imagined to be linearly stored. In contrast, in a multi-dimensional Array, the data arranged is in the form of rows and columns or many rows and columns.

The size of the array determines the dimension of an array. For example, a two-dimensional array would include two sizes that would be arranged in the form of a table. Each element in an array has a unique index that determines its position within the array.

Two-dimensional arrays, three-dimensional arrays, four-dimensional arrays are all multi-dimensional arrays. In fact, a two-dimensional array is the simplest multi-dimensional array. Let us start our discussion with multi-dimensional arrays.

Let us now discuss the initialization of a multi-dimensional array:

Data_type  name_of_array  [size1] [size2] [size3].... [size n];

Also see, Literals in C.

## Two Dimensional Array

As stated earlier, a two-dimensional array is the simplest type of multi-dimensional array. It can be visualized as a table with n number of rows and columns.

Initialization of a two-dimensional array is:

data_type  array_name  [size1] [size2];

An example for this is:

int rank [4] [4];

Number of elements= 4 *4= 16.

### Initialization of a two dimensional Array:

One method of initialization is to assign all the values all at once at the time of declaration of the array.

For example:

int rank [4] [4] = { 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11, 12,13,14,15,16};

Another better way to initialize the array is to club together different elements of a particular row.

int rank [4][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}}

### Accessing Elements in a Two dimensional Array

Let the above table represent the above-defined array. The first element in the array is generally referenced as rank [0][0], the next element in the same row is referenced as rank [0][1]. The elements of the next row can be accessed as rank[1] [0], rank [1][1].

Hence, to access a particular element, we reference it using the index. The syntax for it can be defined as:

array_name [number of row] [number of column];

Example:

rank [3] [4]; // gives us the value of the third row and fourth column

Let us now look at a code segment that helps us understand how are elements of a two-dimensional matrix accessed.

``````#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, j; // intializing variables i, j
// intializing the value of row and column of a matrix
int row = 6;
int column = 4;
int rank[row][column];
// for very column i we go through every row

for (i = 0; i < column; i++) // loop helps us traverse through columns in the array
{
for (j = 0; j < row; j++) // loop helps us traverse through every row in the array
{
rank[i][j] = i + j + 1; // initialising the value of a particular element
}
}
for (i = 0; i < column; i++) // loop helps us traverse through columns in the array
{
for (j = 0; j < row; j++) // loop helps us traverse through every row in the array
{
cout << rank[i][j] << " "; // printing the value of a particular element
}
cout << endl;
}
return 0;
}``````

Output:

``````1 2 3 4 2 3
2 3 4 5 3 4
3 4 5 6 4 5
4 5 6 7 8 9 ``````

Try and compile with online c++ compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Three Dimensional Array

A three-dimensional array consists of three arrays together. Hence, one could visualize it as three tables connected together.

The syntax for declaration of a three-dimensional array is:

data_type array_name [size1] [size2] [size3];

Example:

int matrix [2] [3] [2];

Hence, the total elements will be 2*3*2= 12.

### Initialization of a three-dimensional array:

int rank [2] [3] [2]= { 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11, 12};

However, just as in a two-dimensional array, the better method was to initialize the elements of a row together using curly brackets; we apply the same concept in a three-dimensional array.

int matrix [2][3][2] =

{

{ {1,2}, {3,4}, {5,6} },

{ {7,8}, {9,10}, 11,12} }

};

### Accessing Elements in a three-Dimensional Array:

The elements of a three-dimensional array can be accessed just as we accessed the elements in the two-dimensional arrays.

For example:

int matrix [2] [1] [1];

Next, let us look into printing all the elements in a three-dimensional array.

``````
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, j, k; // intializing variables i, j and k
// intializing the value of row and column of a matrix
int length = 6;
int height = 4;
// for very column i we go through every row

for (i = 0; i < length; i++) // loop helps us traverse through length in the array
{
for (j = 0; j < breadth; j++) // loop helps us traverse through breadth in the array
{
for (k = 0; k < height; k++)  // loop helps us traverse through height in the array
{
rank[i][j][k] = i + j + k + 5;
} // initialising the value of a particular element
}
}
for (i = 0; i < length; i++) // loop helps us traverse through length in the array
{
for (j = 0; j < breadth; j++) // loop helps us traverse through breadth in the array
{
for (k = 0; k < height; k++) // loop helps us traverse through height in the array
{
cout << rank[i][j][k] << "__"; // printing the value of a particular element
}
cout << "<=";
}
cout << endl;
}
return 0;
}``````

Output:

``````5__6__7__8__<=6__7__8__9__<=7__8__9__10__<=8__9__10__11__<=
6__7__8__9__<=7__8__9__10__<=8__9__10__11__<=9__10__11__12__<=
7__8__9__10__<=8__9__10__11__<=9__10__11__12__<=10__11__12__13__<=
8__9__10__11__<=9__10__11__12__<=10__11__12__13__<=11__12__13__14__<=
9__10__11__12__<=10__11__12__13__<=11__12__13__14__<=12__13__14__15__<=
10__11__12__13__<=11__12__13__14__<=12__13__14__15__<=13__14__15__16__<=``````

## FAQs

1. What are the limitations of the Multi-dimensional arrays?
We cannot delete any element from the multi-dimensional arrays.
2. What are the advantages of using Multi-dimensional arrays?
We can use them to represent multiple data items of the same type that too only by using a single name.
3. What are the disadvantages of using Multi-dimensional arrays?
The number of items to be stored in Multi-dimensional arrays should be known beforehand.

## Key Takeaways:

In this blog, we discussed multi-dimensional arrays and various concepts regarding them. Two types of multi-dimensional arrays- Two Dimensional Array and Three Dimensional Arrays were discussed in detail, including methods of accessing various elements in them and printing their values. For learning more about such programming concepts, visit here.

Solve the following problems related to array -

Guided path
Free
Basics of C++
9 chapters
104+ Problems