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

12
34
56
MATRIX

135
246
Transposed Matrix

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:

  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 Check whether given String is Palindrome or not.
  10. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  11. Program to count total number of words in a string.
  12. Program to find smallest and largest word of the String.
  13. Program to find Most Frequent Character of the String.
  14. Program to Remove all the blank spaces from the String.
  15. Program to check if String is isogram or not.
  16. Program to Reverse Each word of the String.
  17. Program to Print All the Substring of the Given String.
  18. Program to find longest palindromic Substring.
  19. Program to check Anagram Strings.
  20. Program to check whether two Strings are Rotation of each other or not.

You may also like...