C++ Program to Find Transpose of a Matrix

C++ program to find transpose of a matrix.


C++ Program to find transpose matrix

#include <iostream>

using namespace std;
 
int main(){
    int rows, cols, r, c;
    int inputMatrix[50][50], transposeMatrix[50][50];
    cout << "Enter Rows and Columns of Matrix" << endl;
    cin >> rows >> cols;
     
    cout << "Enter Matrix of size "<< rows << " X " 
        << cols << endl;
     
    for(r = 0; r < rows; r++){
        for(c = 0; c < cols; c++){
            cin >> inputMatrix[r][c];
        }
    }
     
    for(r = 0; r < rows; r++){
        for(c = 0; c < cols; c++){
            transposeMatrix[c][r] = inputMatrix[r][c];
        }
    }
     
    cout << "Transpose Matrix" << endl;
    for(r = 0; r < cols; r++){
        for(c = 0; c < rows; c++){
            cout << transposeMatrix[r][c] << " ";
        }
        cout << endl;
    }
    return 0;
}

Output
Enter Rows and Columns of Matrix
3 3
Enter Matrix of size 3 X 3
1 2 3
4 5 6
7 8 9
Transpose Matrix
1 4 7
2 5 8
3 6 9

Recommended Posts
C++ Program to Multiply Two Matrices
C++ Program to Add Two Matrix
C++ Program to Add Two Complex Numbers Using Structure
C++ Program to Add Two Distances in Inch and Feet
C++ Program to Store Information of an Employee in Structure
C++ Program to Find Area and Circumference of a Circle
C++ Program to Find Area and Perimeter of Parallelogram
C++ Program to Find Power of Number using Recursion
C++ Program to Check for Armstrong Number
All C++ Programs