How to print Transpose of a matrix in java

0

 Transpose of matrix is one of the basic problem of mathematics . Since it is a problem of matrix then we should try to implement it . It is one of the most asked interview question so please prepare for this question . 

Transpose Matrix :

When we flip one matrix over a diagonal then we get another matrix and that matrix is called transpose matrix .




So if our matrix is

A = { { 1 , 2 } , { 3 , 4 } }

Then transpose of our matrix will be

A(T) = { { 1 , 3 } , { 2 , 4 } } 


Java Code :



public class Matrix
{
    public static void transpose(int[][] mat)
    {
        for(int i=0;i<mat.length;i++)
        {
            for(int j=i+1;j<mat[i].length;j++)
            {
                int tempmat[i][j];
                mat[i][j]= mat[j][i];
                mat[j][i]=temp;
            }
        }
        
    }
    public static void main(String [] args)
    {
        int [][] mat = { {1,2,3},{3,4,5},{7,2,0}};
        System.out.println("------------------");       
        transpose(mat);
        
    }
}




C++ Code :


#include<iostream>

public class Matrix
{
    
    public static void transpose(int[][] mat , int row, int col)
    {
        for(int i=0;i<row;i++)
        {
            for(int j=i+1;j<col;j++)
            {
                int tempmat[i][j];
                mat[i][j]= mat[j][i];
                mat[j][i]=temp;
            }
        }
            
    }
}
int main()
    {
        int [][] mat = { {1,2,3},{3,4,5},{7,2,0}};
        transpose(mat,3,3);
        return 0;
        
    }






Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !