Algorithm


1. Start
2. Read the number of rows (m) and columns (n) of the matrix.
3. Initialize a 2D array/matrices A of size m x n.
4. Read the elements of matrix A.
5. Initialize a new matrix B of size n x m to store the transpose.
6. Loop through each element of matrix A:
   a. Set B[j][i] = A[i][j] for each element A[i][j].
7. Display the original matrix A.
8. Display the transpose matrix B.
9. End

Code Examples

#1 Code Example- Find Transpose of a Matrix

Code - C++ Programming

#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;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
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
Advertisements

Demonstration


C++ Programing Example to Find Transpose of a Matrix-DevsEnv