Program to find Transpose of Matrix in C++

Here is the Program to find Transpose of Matrix in C++

PROGRAM:

#include<iostream>
using namespace std;

int main()
{
        int a[10][10],m,n,i,j;
        cout<<"Enter number of rows: ";
        cin>>m;
        cout<<"Enter number of coloumns: ";
        cin>>n;
        if(m!=n)
        {
                cout<<"Matrix not square so Transpose not possible :(";
        }
        else
        {
                cout<<endl<<"Enter elements of matrix: "<<endl;
                for(i=0;i<m;i++)
                {
                        for(j=0;j<n;j++)
                        {
                                cout<<"Enter element a"<<i+1<<j+1<<": ";
                                cin>>a[i][j];
                        }
                }
                cout<<endl<<"Displaying Matrix: "<<endl<<endl;
                for(i=0;i<m;i++)
                {
                        for(j=0;j<n;j++)
                        {
                                cout<<a[i][j]<<" ";
                        }
                        cout<<endl<<endl;
                }
                cout<<endl<<"Displaying Matrix Transpose: "<<endl<<endl;
                for(i=0;i<m;i++)
                {
                        for(j=0;j<n;j++)
                        {
                                cout<<a[j][i]<<" ";
                        }
                        cout<<endl<<endl;
                }
        }
        return 0;
}

 

Leave a Reply