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.

Example:

N = 3, M = 2

12
34
56
MATRIX

135
246
Transposed Matrix

C++ Program to find transpose of a matrix is as follows:

#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:

  1. Program to Multiply Two Matrix.
  2. Program to check Identity Matrix.
  3. Program to check Idempotent Matrix.
  4. Program to check Involuntary Matrix.
  5. Program to print matrix in zig-zag order.
  6. Program to print matrix in spiral order.
  7. Program to sort a matrix.
  8. Program to Add two Matrix.
  9. Program to find smallest and largest word of the String.
  10. Program to find Most Frequent Character of the String.
  11. Program to Remove all the blank spaces from the String.
  12. Program to check if String is isogram or not.
  13. Program to Reverse Each word of the String.
  14. Program to Print All the Substring of the Given String.
  15. Program to find longest palindromic Substring.
  16. Program to check Anagram Strings.
  17. Program to check whether two Strings are Rotation of each other or not.
  18. Program to check Palindromic Anagram.
  19. Program to print all the Palindromic Substring of the String.
  20. Program to check Panagram String.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *