In this tutorial, we’ll learn about the transpose of matrices (2-D Array) i.e. a program to transpose the matrix using the C++ programming language.
Quick Info:💡
↪ Transpose of matrix means exchanging the rows into column and column into rows.
↪ For example, suppose you have a matrix of [3][3] {(1 2 3),(4 5 6),(7 8 9)} then, the result after transpose of the matrix will be [3][3] {(1 4 7),(2 5 8),(3 6 9)}.
↪ Steps to perform above task:
- First, input the square matrix.
- Second, perform the transpose of matrix i.e. transpose[i][j]==matrix[j][i].
- Third, display the result on the output screen.
Program to Transpose the Matrix (2-D Array)
Example:
#include<iostream.h>
#include<conio.h>
void main()
{
int matrix[20][20],row,col,i,j,transpose[20][20];
cout<<"Enter number of rows of matrix:=";
cin>>row;
cout<<"Enter number of column of matrix:=";
cin>>col;
if(row==col){
cout<<"Enter the elements of array:"<<endl;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
cout<<"Enter the elements of matrix["<<i<<"]["<<j<<"]:";
cin>>matrix[i][j];
}
}
cout<<"Matrix before transposition:";
for(i=0;i<row;i++){
for(j=0;j<col;j++){
cout<<matrix[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"Matrix after transposition:";
for(i=0;i<row;i++){
for(j=0;j<col;j++){
transpose[i][j]=matrix[j][i];
cout<<transpose[i][j]<<"\t";
}
cout<<"\n";
}}
else
cout<<"transpose not possible";
getch();
}


Output:

Working:

Wrapping up:
Well, I hope it really helped you to know the C++ better.
Attention reader⚠Don’t stop learning now.
Just stay with us to get more such exciting codes to explore more in C++.