This
program shows how we can traverse the two dimensional array.When
program will compile it will ask for some input for number of rows and
columns. While you will enter numeric values for the rows and columns,
program will show the output in form of the traverse array.
# include<iostream.h>
# include<iomanip.h>
class Two_Dimension
{
private: int i, j;
float abc[10][10];
public:
void Traverse ( int, int);
void input( int, int);
};
// Display function
void Two_Dimension :: Traverse (int row, int col)
{
cout<<"\n Traversing in row \n";
for( i = 0; i < row; i++)
{
for( j = 0; j < col; j++)
{
cout<<"\n"<<&abc[i][j];
cout<<" "<<abc[i][j];
}
cout<<"\n";
}
cout<<"\n Traversing in column \n";
for( j = 0; j < col; j++)
{
for( i = 0; i < row; i++)
{
cout<<" \n"<<&abc[i][j];
cout<<" "<<abc[i][j];
}
cout<<"\n";
}
cout<<"\n Traversing in row \n";
for( i = 0; i < row; i++)
{
for( j = 0; j < col; j++)
{
cout<<" abc["<<i<<"]"<<"["<<j<<"] = ";
cout<<" "<<abc[i][j];
}
cout<<"\n";
}
cout<<"\n Traversing in column \n";
for( j = 0; j < col; j++)
{
for( i = 0; i < row; i++)
{
cout<<" abc["<<i<<"]"<<"["<<j<<"] = ";
cout<<" "<<abc[i][j];
}
cout<<"\n";
}
}
// Input function
void Two_Dimension :: input( int row, int col)
{
for( i = 0 ; i< row; i++)
{
for( j = 0 ; j<col; j++)
{
cout<<"\nInput Value for : "<< i+1 <<" : "<<j+1<<" : " ;
cin>>abc[i][j];
}
}
}
// main function
void main()
{
Two_Dimension Trav;
int r,c;
cout<<"\n Enter the number of rows:";
cin>>r;
cout<<" Enter the number of columns:";
cin>>c;
Trav.input(r,c);
Trav.Traverse(r,c);
}