C++ program for finding transpose of matrix
#include<iostream>
using namespace std;
//driver function
int main()
{
int matrix[5][5], transpomat[5][5], r, c;
//Taking row number as input from user and storing in r
cout<<"Enter the number of row: ";
cin>>r;
//Taking column number as input from user and storing in c
cout<<"Enter the number of column: ";
cin>>c;
/* Taking elements of matrix as input from user
*/
cout<<"Enter elements: "<<endl;
for(int a =0;a<r;a++) {
for(int b=0;b<c;b++) {
cin>>matrix[a][b];
}
}
// Loop for finding transpose matrix
for(int a=0;a<r;a++) {
for(int b=0;b<c;b++) {
transpomat[b][a] = matrix[a][b];
}
}
//Printing the transpose matrix
cout<<"Transpose of Matrix: "<<endl;
for(int a=0;a<c;a++) {
for(int b=0;b<r;b++) {
cout<<transpomat[a][b]<<" ";
/* Formatting output as a matrix
*/
if(b==r-1)
cout<<endl;
}
}
return 0;
}
Output
Enter the number of row:3
Enter the number of column:3
Enter elements:
1 4 6
2 9 5
6 3 6
Transpose of Matrix:
1 2 6
4 9 3
6 5 6