Sunday 23 February 2014

Transpose of a Matrix - C++ Program Source Code

Here is a program to find the transpose of a matrix. Here is an example for transposal of a matrix:

Example for a matrix transposal


Code:
#include<iostream.h>
#include<conio.h>
void main()
{
    int i,j,m,n,matrix[50][50],transpose[50][50];
    cout<<"Enter the order of the matrix\n";
    cin>>m>>n;
    cout<<"Enter the elements of the matrix \n";
    for(i=0;i<m;i++)
    {
       for(j=0;j<n;j++)
       {
            cin>>matrix[i][j];
       }
    }
    cout<<"The entered matrix is \n";
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
           cout<<matrix[i][j];
           cout<<"  ";
        }
        cout<<"\n";
    }
    //Definition of transpose
    for(i=0;i<n;i++)
    {
       for(j=0;j<m;j++)
       {
            transpose[i][j]=matrix[j][i];
       }
    } 
    cout<<"The transpose is \n";
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
           cout<<transpose[i][j];
           cout<<"  ";
        }
        cout<<"\n";
    }
}

For newer IDEs, visit this page.

Working:

First the user is asked to enter the order and subsequently the elements of a matrix. The given matrix is displayed for formality. The transpose has the number of rows is equal to the number of columns of the first matrix and number of columns equal to the number of rows of the first matrix. It is then defined appropriately and displayed. Here is the output based on the above example.



Happy Coding!

:)

No comments:

Post a Comment

You might also like