Welcome again dear readers! In this post, we will learn to build a c++ program to take the transpose of a matrix. It will help us practice our array's conceptual understanding. It's because a matrix in c++ is actually a two-dimensional array of elements. The program takes the order of the matrix as input. A 2d array will be created with the order of rows*cols. The user will be asked to enter the values of all the array elements. The program will print the entered matrix and its resultant transpose matrix after calculation. Now let's start learning the program.
What is the transpose of a matrix?
A matrix is basically a rectangular array of elements. It is arranged in the form of rows and columns which is called its order. To calculate transpose, we
change all the rows of the matrix into columns or all the columns into rows.
After taking transpose the order of the matrix is reversed. i.e. if we take
the transpose of a 2x3 matrix then the order of its transpose will be
3x2 as shown in the below figure.
How to calculate the transpose of a matrix in c++?
- Declare two variables "rows" and "cols" of integer type and input the order of the matrix from the user.
- Declare a 2d array "matrix" with the same order entered by the user. i.e. matrix[rows][cols].
- Declare another 2d array to store transpose after calculation with the order of 5x5 .i.e. transpose[5][5].
- Take input of all the 2d array elements from the user using nested for loop.
int rows; int cols; cout << "Enter rows of matrix:"; cin >> rows; cout << "enter columns of matrix:"; cin >> cols;
int matrix[rows][cols]; int transpose[5][5]; // input matrix elements for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cout << "Enter the element " << i << " " << j << ":\t"; cin >> matrix[i][j]; } }
- Display the entered matrix to the user using the cout statement and nested for loop.
//display matrix elements cout << "\nThe Entered matrix is" << endl; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cout << matrix[i][j] << " "; } cout << endl; }
- Now calculate the transpose of the matrix by changing the rows into columns. Store rows of "matrix 2d array" as columns in the "transpose 2d array". i.e. transpose[j][i] = matrix[i][j] where index "i" indicates row number and "j" indicates column number.
//calculating transpose for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { transpose[j][i] = matrix[i][j]; } }
- Display the calculated transpose to the user. As we already mentioned, the order of the matrix is reversed after taking transpose. So, to print the resultant transpose matrix, the outer for loop of the nested loop will iterate until "i<cols", and the inner for loop will iterate until "j<rows".
//Printing the tranpose cout << "\nTranspose of the entered matrix" << endl; for (int i = 0; i < cols; ++i) { for (int j = 0; j < rows; ++j) { cout << transpose[i][j] << " "; } cout << endl; }
Also read: Arrays in c++ with code and diagram.
Also read: Print Fibonacci series in c++.
C++ program to calculate the transpose of a matrix?
The complete code of the transpose program in c++ is given below for better comprehension.
#include<iostream> using namespace std; int main() { int rows; int cols; //input the order of the matrix cout << "Enter rows of matrix:"; cin >> rows; cout << "enter columns of matrix:"; cin >> cols; int matrix[rows][cols]; int transpose[5][5]; // input matrix elements for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cout << "Enter the element " << i << " " << j << ":\t"; cin >> matrix[i][j]; } } //display matrix elements cout << "\nThe Entered matrix is" << endl; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cout << matrix[i][j] << " "; } cout << endl; } //calculating transpose for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { transpose[j][i] = matrix[i][j]; } } //Printing the tranpose cout << "\nTranspose of the entered matrix" << endl; for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { cout << transpose[i][j] << " "; } cout << endl; } return 0;
This program can only take the transpose of matrices with the maximum order of
5x5. You can increase it if you want but I think it's sufficient for this example. Now check the output of the program below.
Also read: C++ program to show questions until the user gives the wrong answer.
Also read: C++ important concepts and interview Questions.
0 Comments