Two-Dimensional Array
Two- dimensional array can be considered as a table that consist of rows and columns. Each
element in 2-D array is referred with the help of two indexes. One index is used to indicate the row and
the second index indicates the column of the element.
Syntax
Data-type identifier[row][column];
Int arr[4][3];
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
3,0 3,1 3,2
0 1 2
1 1.Taking input from user in 2D array:
2 #include<iostream>
3 using namespace std;
int main()
{
int arr[2][4];
for(int i=0;i<2;i++)
for(int j=0;j<4;j++)
{
cout<<"Enter an integer: ";
cin>>arr[i][j];
}
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
return 0;
}
2.Initializing 2D array:
#include<iostream>
using namespace std;
int main()
{
int arr[2][3]={15,21,9,84,33,72};
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
cout<<"arr["<<i<<"]["<<j<<"]= "<<arr[i][j]<<" ";
cout<<endl;
}
return 0;
}
3.Find Maximum & Minimum in 2D array:
#include<iostream>
using namespace std;
int main()
int max,min,arr[2][4]={{15,21,9,84},{33,72,18,47}};
max=min=arr[0][0];
for(int i=0;i<2;i++)
for(int j=0;j<4;j++)
if(arr[i][j]>max)
max=arr[i][j];
if(arr[i][j]<min)
min=arr[i][j];
cout<<"Maximum= "<<max<<endl<<"Minimum= "<<min;
return 0;