Transpose of a Matrix
“Transpose of a matrix” is a basic problem on matrix where we interchange the row and columns with each other. Here, we are given a matrix of size N*M and our task is to find the transpose of the matrix by interchanging the row and columns.
The resultant matrix will be of size M*N.
Example:
N = 3, M = 2
1 | 2 |
3 | 4 |
5 | 6 |
1 | 3 | 5 |
2 | 4 | 6 |
C++ Program to find transpose of a matrix is as follows:
/* Program to find transpose of a Matrix */
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N,M;
// Scan Dimensions of the matrix
cout<<"Enter the dimensions of the matrix: ";
cin>>N>>M;
int matrix[N][M];
int transpose[M][N];
// Scan Matrix Elements
cout<<"Enter the elements of first matrix:";
for(int i = 0 ; i < N ; i++)
for(int j = 0 ; j < M ; j++)
cin>>matrix[i][j];
// Transpose Matrix
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j)
transpose[j][i] = matrix[i][j];
//Print Transposed matrix
cout<<"The Transposed Matrix is:\n";
for(int i = 0 ; i < M ; i++)
{
for(int j = 0 ; j < N ; j++)
{
cout<<transpose[i][j]<<" ";
}
cout<<endl;
}
}
OUTPUT: Enter the dimensions of the matrix: 3 2 Enter the elements of matrix: 1 2 3 4 5 6 The Transposed Matrix is: 1 3 5 2 4 6
Related Posts:
- Program to Multiply Two Matrix.
- Program to check Identity Matrix.
- Program to check Idempotent Matrix.
- Program to check Involuntary Matrix.
- Program to print matrix in zig-zag order.
- Program to print matrix in spiral order.
- Program to sort a matrix.
- Program to Add two Matrix.
- Program to Check whether given String is Palindrome or not.
- Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
- Program to count total number of words in a string.
- Program to find smallest and largest word of the String.
- Program to find Most Frequent Character of the String.
- Program to Remove all the blank spaces from the String.
- Program to check if String is isogram or not.
- Program to Reverse Each word of the String.
- Program to Print All the Substring of the Given String.
- Program to find longest palindromic Substring.
- Program to check Anagram Strings.
- Program to check whether two Strings are Rotation of each other or not.