![]() |
| Multi Dimensional Array in C++ | 2D Array Complete Guide |
What is Multidimensional Array
2-D Array in C: A two-dimensional array in C++ is a data structure that can hold multiple values arranged in row and columns using square brackets. Array is a collection of data of same data types. It can store data of same data types means an integer array can store integer value and character array can store character value.
Array can be used to group of data simultaneously. We can not fetch data from array value directly therefore we can use index point value.
The indexing of array always start with row 0 and column index value is always integer value. Arrays is allow to store multiple values of the same data type in a single variable.
Two Dimensional Array in C++ Syntax
- Data_type: The array data type of data that the array will hold (int, float, char, etc.).
- Array_name: The name you choose for the 2D array.
- Row: The number of rows in the 2D array.
- Columns: The number of columns in the array.
int a[3][3]={{10,20,30},{40,50,60},{70,80,90}}; |
C++ 2D Array Initialization
Array is the are several ways to initialize 2D arrays. The choice of initialization method depends on the requirements of your C++ program. Here are some common methods for initializing 2D arrays in C++.
C++ 2 Dimensional Array Initialization
a[0][0]=10 a[0][1]=20 a[0][2]=30 a[1][0]=40 a[1][1]=50 a[1][2]=60 a[2][0]=70 a[2][1]=80 a[2][2]=90 |
The values to initialize the array with the number of elements inside the curly braces must match the size of the array.
We declare and initialize an integer array numbers with a size of row 3 and column size 3 and we provide 9 values.
Multidimensional Array in C++ Example Program
| #include<iostream> int main(){ int a[3][3]={{10,20,30},{40,50,60},{70,80,90}}; cout<<"Value of a[0][0]:"<<a[0][0]<<endl; cout<<"Value of a[0][1]:"<<a[0][1]<<endl; cout<<"Value of a[0][2]:"<<a[0][2]<<endl; cout<<"Value of a[1][0]:"<<a[1][0]<<endl; cout<<"Value of a[1][1]:"<<a[1][1]<<endl; cout<<"Value of a[1][2]:"<<a[1][2]<<endl; cout<<"Value of a[2][0]:"<<a[2][0]<<endl; cout<<"Value of a[2][1]:"<<a[2][1]<<endl; cout<<"Value of a[2][2]:"<<a[2][2]<<endl; } ============OUTPUT============ Value of a[0][0]:10 Value of a[0][1]:20 Value of a[0][2]:30 Value of a[1][0]:40 Value of a[1][1]:50 Value of a[1][2]:60 Value of a[2][0]:70 Value of a[2][1]:80 Value of a[2][2]:90 |
C++ Loop Through Multidimensional Array
int main(){ int i,j; int a[3][3]={{10,20,30},{40,50,60},{70,80,90}}; for(int i=0;i<3;i++){ for(j=0;j<3;j++) { cout<<"Value of a:["<<i<<"]["<<j<<"]:"<<a[i][j]<<endl; } } } ============OUTPUT============ Value of a[0][0]:10 Value of a[0][1]:20 Value of a[0][2]:30 Value of a[1][0]:40 Value of a[1][1]:50 Value of a[1][2]:60 Value of a[2][0]:70 Value of a[2][1]:80 Value of a[2][2]:90 |
Two Dimensional Array in C++ Example with Output
int main(){ int i,j; int a[3][3]={{10,20,30},{40,50,60},{70,80,90}}; for(int i=0;i<3;i++){ for(j=0;j<3;j++) { cout<<" "<<a[i][j]; } cout<<endl; } } ============OUTPUT============ 10 20 30 40 50 60 70 80 90 | 10 20 30 | 40 50 60 | 70 80 90 |
C++ Multidimensional Array Size
C++ Multidimensional Array Length
int main(){ int i,j; int a[5][5] = {{20, 48, 87,56,68}, {56, 19,10,89,80},{54,56,34,68,45},{67,45,76,34,68},{44,34,56,77,55}}; for(i=0;i<5;i++){ for(j=0;j<5;j++){ cout<<" "<<a[i][j]; } cout<<endl; } } ============OUTPUT============ 20 48 87 56 68 56 19 10 89 80 54 56 34 68 45 67 45 76 34 68 44 34 56 77 55 |
C++ Dynamic 2D array Initialization
Create 2D Array Dynamically in C++
int main(){ int a[3][3]; int i,j,r,c; cout<<"Enter Row Size:"; cin>>r; cout<<"Enter Colum Size:"; cin>>c; for(i=0;i<r;i++){ for(j=0;j<c;j++){ cout<<"Enter 2D Array Element["<<i<<"]["<<j<<"]:"; cin>>a[i][j]; } } cout<<"Your 2D Matrix"<<endl; for(i=0;i<r;i++){ for(j=0;j<c;j++){ cout<<" "<<a[i][j]; } cout<<endl; } } ============OUTPUT============ Enter Row Size:3 Enter Column Size:3 Enter 2D Array Elements[0][0]:12 Enter 2D Array Elements[0][1]:13 Enter 2D Array Elements[0][2]:45 Enter 2D Array Elements[1][0]:67 Enter 2D Array Elements[1][1]:87 Enter 2D Array Elements[1][2]:45 Enter 2D Array Elements[2][0]:78 Enter 2D Array Elements[2][1]:98 Enter 2D Array Elements[2][2]:12 Your 2D Matrix 12 13 45 67 87 45 78 98 12 |
Update 2D Array Elements in C++
How To Update 2D Array Element in C++
int main(){ int i,j,a[3][3] = {{10, 28, 80},{56, 90, 20},{20,60,70}}; cout<<"Before Update 2D Matrix"<<endl; for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++){ cout<<" "<<a[i][j]; } cout<<endl; } cout<<"After Update 2D Matrix"<<endl; a[1][0] = 99; for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++){ cout<<" "<<a[i][j]; } cout<<endl; } } ============OUTPUT============ Before Update 2D Matrix 10 28 80 56 90 20 20 60 70 After Update 2D Matrix 10 28 80 99 90 20 20 60 70 |
Addition of Two Matrix in C++ using 2D Array
int main(){ int x[3][3]; int y[3][3]; int z[3][3]; int i,j,r,c; cout<<"Enter First Matrix Row Size:"; cin>>r; cout<<"Enter First Matrix Colum Size:"; cin>>c; for(i=0;i<r;i++){ for(j=0;j<c;j++){ cout<<"Enter First Matrix Array Element["<<i<<"]["<<j<<"]:"; cin>>x[i][j]; } } cout<<"Your First Matrix"<<endl; for(i=0;i<r;i++){ for(j=0;j<c;j++){ cout<<" "<<x[i][j]; } cout<<endl; } cout<<"Enter Second Matrix Row Size:"; cin>>r; cout<<"Enter Second Matrix Colum Size:"; cin>>c; for(i=0;i<r;i++){ for(j=0;j<c;j++){ cout<<"Enter Second Matrix Array Element["<<i<<"]["<<j<<"]:"; cin>>y[i][j]; } } cout<<"Your Second Matrix"<<endl; for(i=0;i<r;i++){ for(j=0;j<c;j++){ cout<<" "<<y[i][j]; } cout<<endl; } cout<<"Addition of Two Matrix"<<endl; for(i=0;i<r;i++){ for(j=0;j<c;j++){ z[i][j]=x[i][j]+y[i][j]; //sign change create more program. cout<<" "<<z[i][j]; } cout<<endl; } } ============OUTPUT============ Enter First Matrix Row Size:2 Enter First Matrix Colum Size:2 Enter First Matrix Array Element[0][0]:1 Enter First Matrix Array Element[0][1]:2 Enter First Matrix Array Element[1][0]:3 Enter First Matrix Array Element[1][1]:4 Your First Matrix 1 2 3 4 Enter Second Matrix Row Size:2 Enter Second Matrix Colum Size:2 Enter Second Matrix Array Element[0][0]:5 Enter Second Matrix Array Element[0][1]:6 Enter Second Matrix Array Element[1][0]:7 Enter Second Matrix Array Element[1][1]:8 Your Second Matrix 5 6 7 8 Addition of Two Matrix 6 8 10 12 |

