Multi-dimensional Arrays in C/C++ (2D & 3D Arrays) – Unveil the Important Concepts

Get Certified in C Programming for Free and Take Your Skills to the Next Level

In our previous article, we talked about arrays in C and C++. Now, it’s time to uncover the secrets of Multi-dimensional Arrays in C/C++ programming language. As the name itself suggest, arrays that possess more than one dimension are multi-dimensional arrays. In this tutorial, we will restrict our focus on two-dimensional and three-dimensional arrays as they are the most commonly used and applied multi-dimensional arrays in C and C++.

Multi-dimensional Array in C/C++

The arrays of an array are known as multi-dimensional arrays. It is a pretty clear and comprehensible concept to grasp.

Multi-dimensional Array in C and C++

1. 2D Arrays

An array of an array is referred to as a two-dimensional array. In simpler words, it is a sequence of strings within a sequence of strings.

In order to understand the working of 2D arrays in C/C++, let us begin by discussing its basic syntax-

1.1 Declaration of 2D arrays in C/C++

return_type array_name [ size of row ] [ size of column ];

For instance,

int matrix [3] [3];

If you aren’t aware of the Syntax of C, then you can’t afford to miss checking out the Basic Syntax Rules in C

Here is a diagram, which would help you visualize how a matrix looks like:

The name of the 2-D array is a matrix of the integer data type with 3 rows and 4 columns.

2D array in C and C++

Clearly, from the table, we observe that there would be 12 elements in the matrix, which is the product of the size of its rows and columns.

1.2 Initialization of 2D arrays in C/C++

Instead of simply declaring the multi-dimensional arrays, the C programming language gives you the provision to initialize it as well.

Let us consider an example of a 2D array for simplicity sake.

We can do in it multiple ways-

  • int array [2] [4] = { {2, 4, 0, -2 }, {-1, 4, -7, 0} };
  • int array [2] [4] = { 2, 4, 0, -2 -1, 4, -7, 0};
  • int array [ ] [4] = { {2, 4, 0, -2 }, {-1, 4, -7, 0} };

The 2D array would have 2 rows and 4 columns in each case.

Similarly, we can initialize multi-dimensional arrays in a similar manner of nth dimension

int array [ ][ ][ ]….nth[ ] = { { { …nth { values } } } …..nth }

Don’t forget to check Data Types in C Language

1.3 How to define 2D array in C?

If you are working with the for loop, you require 2 for loops to store the 2D array. One of the most common features of the 2D array is the implementation of matrices.

In mathematics, the matrix is a rectangular array of elements (numbers or expressions), that is represented with the help of rows and columns.

The basic concept behind a matrix is the same in programming. In addition to that, we can call it a grid useful in storing, displaying and manipulating data items.

Here is a simple code in C which illustrates how to take a matrix input and display it:

#include<stdio.h>
void main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int matrix[10][10];
int no_of_rows, no_of_columns;
int iteration1, iteration2;

printf("Enter the number of rows of the matrix: ");
scanf("%d",&no_of_rows);
printf("Enter the number of columns of the matrix: ");
scanf("%d",&no_of_columns);

printf("Enter the elements of the matrix:\n");
for(iteration1 = 0; iteration1 < no_of_rows; iteration1++ )
{
for(iteration2 = 0; iteration2 < no_of_columns; iteration2++)
{
scanf("%d", &matrix[iteration1][iteration2]);
}
}
printf("The matrix is:\n");
for(iteration1 = 0; iteration1 < no_of_rows; iteration1++ )
{
for(iteration2 = 0; iteration2 < no_of_columns; iteration2++)
{
printf("%d\t",matrix[iteration1][iteration2]);
}
printf("\n");
}
}

Code on Screen-

2D arrays in C with Example

Output-

2d arrys output

1.4 How to define 2D array in C++?

Here is a simple code in C++ that illustrates how to take a matrix input and display it:

#include <iostream>
using namespace std;

int main()
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

int matrix[10][10];
int no_of_rows, no_of_columns;
int iteration1, iteration2;

cout<<"Enter the number of rows of the matrix: ";
cin>>no_of_rows;
cout<<"Enter the number of columns of the matrix: ";
cin>>no_of_columns;

cout<<"Enter the elements of the matrix: "<<endl;
for(iteration1 = 0; iteration1 < no_of_rows; iteration1++ )
{
for(iteration2 = 0; iteration2 < no_of_columns; iteration2++)
{
cin>>matrix[iteration1][iteration2];
}
}
cout<<"The matrix is: "<<endl;
for(iteration1 = 0; iteration1 < no_of_rows; iteration1++ )
{
for(iteration2 = 0; iteration2 < no_of_columns; iteration2++)
{
cout<< "\t" <<matrix[iteration1][iteration2];
}
cout<<endl;
}
return 0;
}

Code-

Define 2D array in C++

Output-

Output of 2D array in C++

2. 3D Arrays

Three-dimensional arrays in C/C++ are referred to as an array of arrays. Its syntax is similar to a 1D or 2D array:

return_type array_name [ size-1] [ size-2 ][ size-3];

For instance,

int sample [3] [2] [3] ;

3D Arrays

Clearly, from the diagram, we observe that there would be 18 elements in the 3D array, which is the product of dimensions (3*2*3) of the array.

If you are working with the for loop, you require 3 for loops to store the 3D array.

2.1 How to define 3D arrays in C?

Here is a simple code in C which illustrates the use of a three-dimensional matrix:

#include <stdio.h>
void main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int i, j, k, sample[3][2][3], size;
size=3*2*3; // Size of the 3D array is the product of size of each array
printf("Enter %d elements: \n",size);

for(i = 0; i < 3; ++i)
{
for (j = 0; j < 2; ++j)
{
for(k = 0; k < 3; ++k )
{
scanf("%d", &sample[i][j][k]);
}
}
}
printf("The values are:\n\n"); // To display the values of elements according to their index
for(i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
for(k = 0; k < 3; k++)
{
printf("sample[%d][%d][%d] = %d\n", i, j, k, sample[i][j][k]);
}
}
}
}

Code on Screen-

 3D arrays in C with example

Output-

3d Arrays in C output

2.2 How to define 3D arrays in C++?

#include <iostream>
using namespace std;

int main()
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

int i, j, k, sample[3][2][3], size;
size=3*2*3; // Size of the 3D array is the product of size of each array
cout<<"Enter "<< size << " elements "<<endl;

for(i = 0; i < 3; ++i)
{
for (j = 0; j < 2; ++j)
{
for(k = 0; k < 3; ++k )
{
cin>>sample[i][j][k];
}
}
}
cout<<"The values are: "<<endl; // To display the values of elements according to their index
for(i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
for(k = 0; k < 3; k++)
{
cout<<"sample [ "<< i <<" ][ "<< j << "] [ " << k << " ] = "<< sample[i][j][k] <<endl;
}
}
}
return 0;
}

Code-

define 3D arrays in C++

Output-

define 3D arrays in C++

Summary

In this tutorial, we discussed 2D and 3D arrays in C/C++, how are they declared, their indexing, and how are they stored and displayed. It has various applications in algorithms of:

  • Bubble sort
  • Binary search
  • Matrix manipulations that is Strassen’s matrix multiplication and many more.

Therefore, learning about multi-dimensional arrays in C/C++ would give you an upper edge over programmers who are alien to the concept of multi-dimensional arrays.

Ready for the next topic? Jump to Recursion in C/C++

Suggestions and feedbacks related are welcomed in the comment section!

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

1 Response

  1. RAK ACOUSTIC says:

    nice

Leave a Reply

Your email address will not be published. Required fields are marked *