Program to perform Transpose of a Matrix in C++
Following is the program to perform transpose of a matrix.
#include<iostream.h>
void main()
{
int mat1[3][3], mat2[3][3];
int i, j, k;
cout<<"Enter the elements of Matrix(3X3) : ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat1[i][j];
}
}
cout<<"\nMatrix is : "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<mat1[i][j]<<" ";
}
cout<<endl;
}
//Transposing Matrices
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mat2[i][j]=mat1[j][i];
}
}
cout<<"\nTransposed matrix is : "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<mat2[i][j]<<" ";
}
cout<<endl;
}
}
Enter the elements of Matrix(3x3) : 1
2
3
4
5
6
7
8
9
Matrix is :
1 2 3
4 5 6
7 8 9
Transposed Matrix is :
1 4 7
2 5 8
3 6 9