C++ Program for transpose of matrix

Transpose of a matrix is a matrix whose columns are rows of original matrix and rows are columns of original matrix.
For example if a matrix m is


                                    1  1  1  1
      m[3][4]  =                2  2  2  2
                                    3  3  3  3
then transpose matrix t will be

                                    1  2  3
   t[4][3] =                     1  2  3
                                    1  2  3
                                    1  2  3

C++ Program to find transpose of a matrix.


/* C++ Program to find transpose of a matrix */
#include<iostream>
using namespace std;
int main()
{
 int n, m;
 cout<<"Enter rows of a matrix: ";
 cin>>m;
 cout<<"Enter column of matrix: ";
 cin>>n;
 int i,j, matrix[m][n], t[n][m];
 
 cout<<endl<<"Enter matrix elements: "<<endl;
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   cin>>matrix[i][j];
  }
 }
 
 cout<<"Matrix is: "<<endl;
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  {
   cout<<matrix[i][j]<<"  ";
  }
  cout<<endl;
 }
 /* convert matrix into matrix^T */
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
  {
   t[i][j] = matrix[j][i];
  }
 
  } 
 
  /* print transpose of matrix */
  cout<<"Transpose of matrix: "<<endl;
  for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
   {
    cout<<t[i][j]<<"  ";
   }
   cout<<endl;
  }
 
  return 0;
}

Popular posts from this blog