# C++ Program to Find Transpose of a matrix

February 9, 2023, Learn eTutorial
1003

This is a C++ program that takes a matrix from the user and computes the transpose.

## Transpose of a matrix

The Transpose of a matrix can be calculated by interchanging the rows and columns, ie the elements of the rows are written as column, and elements in a column will be written in a row of the matrix transpose. We can denote the matrix transpose by using the letter “T” in the superscript. For example, if “S” is the matrix, then the transpose of 'S' is represented by S' or ST.

## How to find the transpose of a matrix?

For finding a matrix transpose, interchange the row elements and column elements with each other.

## C++ program to find the transpose of a matrix

In this C++ program, the user enters the number of rows and columns. Read these values into the variables row and column. Then, prompt the user to enter some elements for the matrix and store those elements into an array a[10][10] by using a `nested for loop`.

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. 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] that is aT[j][i] = a[i][j] using another nested for loopDisplay 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
```

VIEW ALL
VIEW ALL