C++ Programming Code Examples C++ > Arrays and Matrices Code Examples Program to Find Transpose of a Matrix Program to Find Transpose of a Matrix This program takes a matrix of order r*c from the user and computes the transpose of the matrix. In this program, user is asked to entered the number of rows r and columns c. The value of r and c should be less than 10 in this program. The user is asked to enter elements of the matrix (of order r*c). Then, the program computes the transpose of the matrix and displays it on the screen. #include <iostream> using namespace std; int main() { int a[10][10], trans[10][10], r, c, i, j; cout << "Enter rows and columns of matrix: "; cin >> r >> c; // Storing element of matrix entered by user in array a[][]. cout << endl << "Enter elements of matrix: " << endl; for(i = 0; i < r; ++i) for(j = 0; j < c; ++j) { cout << "Enter elements a" << i + 1 << j + 1 << ": "; cin >> a[i][j]; } // Displaying the matrix a[][] cout << endl << "Entered Matrix: " << endl; for(i = 0; i < r; ++i) for(j = 0; j < c; ++j) { cout << " " << a[i][j]; if(j == c - 1) cout << endl << endl; } // Finding transpose of matrix a[][] and storing it in array trans[][]. for(i = 0; i < r; ++i) for(j = 0; j < c; ++j) { trans[j][i]=a[i][j]; } // Displaying the transpose,i.e, Displaying array trans[][]. cout << endl << "Transpose of Matrix: " << endl; for(i = 0; i < c; ++i) for(j = 0; j < r; ++j) { cout << " " << trans[i][j]; if(j == r - 1) cout << endl << endl; } return 0; }