C++ Program to Find Transpose of a matrix

This is a program that takes a matrix from the user and computes the transpose of the matrix and displays it on the screen.

Transpose of a matrix

The transposition of a matrix is found by interchanging its rows into columns or columns into rows. The transpose of the matrix is denoted by using the letter “T” in the superscript of the given matrix. For example, if “A” is the given matrix, then the transpose of the matrix is represented by A' or AT.

How to find the transpose of a matrix?

To calculate the transpose of a matrix, simply interchange the rows and columns of the matrix i.e. write the elements of the rows as columns and write the elements of a column as rows.
 

C++ program to find the transpose of a matrix

The program asks the user to enter the number of rows and columns. Read these values into the variables row and column. In this program, the value of row and column should be less than 10. Then, the user is asked to enter the elements of the matrix.
The entered elements are stored into an array a[10][10] by using a nested for loop.
Initialize variable i for row and j for column. Set value of both variables to 0. And increment the value of i and j after each execution.
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];
      }

   }
 Display the entered matrix on the screen by calling the array a[i][j] from the initial position to the final. Nested for loop can be used for this. Check for the condition j = column-1. If the condition is true then exit from the loop. If condition statement can be used to perform the checking operation.
Now compute the transpose of the entered matrix. For this declare another array called aT[10][10]. Call the original matrix a[10][10] and store the values interchangeably to the array matrix aT[10][10]. The value in the row of a[10][10] is stored in the column of the aT[10][10].
aT[j][i] = a[i][j].
Display the resultant matrix on the screen as we display the original matrix above.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare integer type variables row , column, i, j  and int type array  a[10][10],                   aT[10][10]

Step 5: Ask the user to enter the number of rows and columns ( 1 to 10 ).

Step 6: read the numbers into the variable row and column;

Step 7: Ask the user to enter the elements 

Step 8: Store the matrix elements into the array a[10][10] by using a nested for loop.

Step9: Display the matrix a[10][10] on the screen.

Step 10: Compute the transpose of the matrix a[i][j]. aT[j][i] = a[i][j]

Step 11:Display the transposed matrix on the screen.

Step 12: exit.
 

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {
   int a[10][10], aT[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) {
         aT[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 << " " << aT[i][j];
         if (j == row - 1)
            cout << endl << endl;
      }

   return 0;
}
                                      

OUTPUT

Enter rows and columns of matrix: 2
4
Enter elements of matrix: 
Enter element a11: 1
Enter element a12: 2
Enter element a13: 3
Enter element a14: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 7
Enter element a24: 8
Entered Matrix: 
 1 2 3 4

 5 6 7 8


Transpose of Matrix: 
 1 5

 2 6

 3 7

 4 8