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:
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
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++.
View related posts:
- C++ Program to Add Two Matrices (Multi-dimensional Array)
- C++ Program to Add Two Numbers
- C++ Program to Calculate the Simple Interest
- C++ Program to Check Armstrong Number
- C++ Program to Check Palindrome Number
- C++ Program to Check Whether a Character is Vowel or Consonant
- C++ Program to Demonstrate the Arithmetic Operators
- C++ Program to Demonstrate the Assignment Operator
- C++ Program to Display Multi-Dimensional Array
- C++ Program to Display Single Dimensional Array
- C++ Program to Find Factorial of a Number
- C++ Program to Find Out Product of Digits
- C++ Program to Find Out Sum of Digits
- C++ Program to Find Out the Area of Square
- C++ Program to Find the Fibonacci Sequence
- C++ Program to Find the Largest Among Three Numbers
- C++ Program to Reverse a Number
- C++ Program to Swap Two Numbers
- C++ Program to Transpose the Matrix (2-D Array)
Leave a Reply