C++ Program to Display Multi-Dimensional Array

Last updated on:

In this tutorial, we will learn about the multi-dimensional array in C++ i.e. how to input and print the multi-dimensional array using C++.

But, before that, it’s important to know what actually multi-dimensional array is. and what is its syntax?

Quick Info:💡

↪ As we know array is of two types i.e. single dimensional array and multi-dimensional array.

↪ Multi dimensional array includes the 2-Dand 3-D representation of arrays.

↪ Multi dimensional array can be represented using the columns and rows.

  • For example:
multidimensional array

Program to Display Multi-Dimensional Array

Here we will learn about 2 dimensional array in C++.

Syntax:

datatype nameofarray[row][column];
  • datatype- It is a data type of the value that is stored in the array.
  • nameofarray- It is the name of the array.
  • row- The first index of the array element represents the row i.e. a[m][n], where m is a row size.
  • column- The second index of the array element represents the column i.e. a[m][n], where n is a column size.

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
int i, j, arr[3][3];
cout<<"Enter the elements of array:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cout<<"Enter the elements of arr["<<i<<"]["<<j<<"]:";
cin>>arr[i][j];
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cout<<arr[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}
//Output:
Enter the elements of array:
Enter the element of arr[0][0]:1
Enter the element of arr[0][1]:2
Enter the element of arr[0][2]:3
Enter the element of arr[1][0]:4
Enter the element of arr[1][1]:5
Enter the element of arr[1][2]:6
Enter the element of arr[2][0]:7
Enter the element of arr[2][1]:8
Enter the element of arr[2][2]:9
1 2 3
4 5 6
7 8 9
Multi dimensional array

Working:

Multi dimensional array working

Wrapping Up:

Well, I hope it really helped you to know C++ better.

Attention reader⚠ Don’t stop learning now.

Just stay with us to get more such exciting codes to explore more in C++.

Comments

Leave a Reply

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