Find Transpose of a Matrix in C++

Transpose of a matrix is nothing but exchanging the rows and columns of a matrix. We can transpose a matrix in C++ language. Generally matrix is called the elements arranged in rectangular array of rows and columns.

We can add, subtract, multiply, divide two matrixes in C++ language. For loop is the main function to transpose a matrix in C++language.

Find Transpose of a Matrix in C++

Find Transpose of a Matrix in C++

Source Code of Transpose of a Matrix in C++

We manually set the size of the matrix should be less than 10 in the below program. We have to enter the number of rows and elements less than 10. The user asks you to enter the elements of the matrix. It exchanges the rows and columns of the entered matrix and displays the transpose matrix.

#include <iostream>
using namespace std;

int main() {
   int a[10][10], transpose[10][10], row, column, i, j;

   cout << "Enter rows and columns of matrix: ";
   cin >> row >> column;

   cout << "\nEnter elements of matrix: " << endl;

   // Storing matrix elements
   for (int i = 0; i < row; ++i) {
      for (int j = 0; j < column; ++j) {
         cout << "Enter element a" << i + 1 << j + 1 << ": ";
         cin >> a[i][j];
      }
   }

   // Printing the a matrix
   cout << "\nEntered Matrix: " << endl;
   for (int i = 0; i < row; ++i) {
      for (int j = 0; j < column; ++j) {
         cout << " " << a[i][j];
         if (j == column - 1)
            cout << endl << endl;
      }
   }

   // Computing transpose of the matrix
   for (int i = 0; i < row; ++i)
      for (int j = 0; j < column; ++j) {
         transpose[j][i] = a[i][j];
      }

   // Printing the transpose
   cout << "\nTranspose of Matrix: " << endl;
   for (int i = 0; i < column; ++i)
      for (int j = 0; j < row; ++j) {
         cout << " " << transpose[i][j];
         if (j == row - 1)
            cout << endl << endl;
      }

   return 0;
}
Read Also : Find Length of  a String in C++

Output

As an example I enter 2 rows and 3 columns here. The transpose matrix has 3 rows and 2 columns.

Enter rows and columns of matrix: 2
3

Enter elements of matrix:
Enter element a11: 1
Enter element a12: 2
Enter element a13: 9
Enter element a21: 0
Enter element a22: 4
Enter element a23: 7

Entered Matrix:
1  2  9

0  4  7

Transpose of Matrix:
1  0

2  4

9  7

Final Words

I hope this article helps you to Find Transpose of a Matrix in C++ Program. If you face any issues please let me know via the comment section. Share this article with other C++ program developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment