Program for transpose of a matrix

Transpose of a matrix is the property to interchange rows and columns of matrix i.e row will become the column of resultant matrix and column will become the row of resultant matrix.

Suppose we have matrices of order 3×3.

Matrix A :

1     2     3

4     5     6

7     8     9

then transpose of Matrix A will be

1     4     7

2     5     8

3     6     9

In transpose, order of square matrix remains same but order of matrix is in m x n order

then after transpose it changes to order n x m.

 

//header files

#include<iostream.h>

#include<conio.h>

int main()

{

// to clear the screen

clrscr();

//initialize variables

int a[10][10];

int b[10][10];

int x,y,i,j;

// to take input from user

cout<<“\nEnter the number of rows and columns :::\n\n”;

cin>>x>>y;

cout<<“\n\nEnter elements for Matrix A :::\n\n”;

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cin>>a[i][j];

}

cout<<“\n”;

}

cout<<“\n\nMatrix A :\n\n”;

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<“\t”<<a[i][j];

}

cout<<“\n\n”;

}

for(i=0;i<y;i++)

{

for(j=0;j<x;j++)

{

b[i][j]=a[j][i];

}

}

cout<<“\n\nTranspose of Matrix A :\n\n”;

for(i=0;i<y;i++)

{

for(j=0;j<x;j++)

{

cout<<“\t”<<b[i][j];

}

cout<<“\n\n”;

}

getch();

return 0;

}