The transpose of a matrix is obtained by interchanging its rows and column.
Algorithm:
1. Take input for number of rows and columns.
2. Interchange rows and columns.
Below given code doesn’t use functions. It can also be written using functions.
#include<iostream> using namespace std; int row,column; int main() { int value,row,column; //insert rows and columns for matrix cout<<"Number of rows :"; cin>>row; cout<<"Number of columns :"; cin>>column; int a[row][column]; //insert values in the matrix cout<<"Insert value in matix :"; cout<<"\n a: \n"; for(int i=0;i<row;i++) { for(int j=0;j<column;j++) { cin>>value; a[i][j]=value; } } //puting content of rows of a in columns of b int b[column][row]; for(int j=0;j<row;j++) { for(int i=0;i<column;i++) b[i][j]=a[j][i]; } //copy contents of b in a for(int i=0;i<column;i++) { for(int j=0;j<row;j++) a[i][j]=b[i][j]; } cout<<"Transpose of matrix is:"<<endl; //display a for(int i=0;i<column;i++) { for(int j=0;j<row;j++) cout<<a[i][j]<<" "; cout<<"\n"; } return 0; }
0 Comments