## Question Reference

C++ Home

### C++ Inbuilt Functions

C++ String Functions

### C++ Interview Programs

C++ Common ProgramsC++ Array ProgramsC++ Converter ProgramsC++ Star Patterns ProgramC++ Number Patterns ProgramC++ Alphabet Patterns Program

ASCII Table

# C++ Program to perform Matrix Transpose

Last Updated:

## Perform Matrix Transpose

In the following example, we will transpose the given matrix (two-dimensional array).

### Example

C++ Compiler
#include <iostream> using namespace std; int main() { int i, j, n; int arr[3][3] = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3} }; cout << "Matrix (3 x 3):\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cout << arr[i][j] << " "; cout <<"\n"; } for(i=0; i<3; i++) { for(j=i+1; j<3; j++) { n = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = n; } } cout << "\nMatrix Transpose: \n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cout << arr[i][j] << " "; cout <<"\n"; } return 0; }

In the following example, we will get the values for (3 x 3) Matrix from the user and display the transpose matrix.

### Example

C++ Compiler
#include <iostream> using namespace std; int main() { int i, j, n, arr[3][3]; cout << "Enter Matrix (3 x 3):\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cin >> arr[i][j]; } for(i=0; i<3; i++) { for(j=i+1; j<3; j++) { n = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = n; } } cout << "\nMatrix Transpose: \n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) cout << arr[i][j] << " "; cout <<"\n"; } return 0; }