Write C++ Program to Find the Transpose of a given Matrix

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
using namespace std;
 
int main()
{
    static int array[10][10];
    int i, j, m, n;
 
    cout<<"Enter the order of the matrix \n";
    // Inputing elements in matrix from user
    cin>>m>>n;
    cout<<"Enter the coefiicients of the matrix\n";
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            cin>>array[i][j];
        }
    }
    //Printing the original matrix
    cout<<"The given matrix is \n";
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            cout<<" "<<array[i][j];
        }
        cout<<"\n";
    }
    //Printing the transpose of matrix
    cout<<"Transpose of matrix is \n";
    for (j = 0; j < n; ++j)
    {
        for (i = 0; i < m; ++i)
        {
           cout<<" "<<array[i][j];
        }
        cout<<"\n";
    }
    return 0;
}
 
 

Result

Write C++ Program to Find the Transpose of a given Matrix
Write C++ Program to Find the Transpose of a given Matrix

Leave a Comment