Program to find the transpose of a matrix

Problem Statement:- Program to find the transpose of a matrix.

Sample Input/Output:-


Sample Input First:2 2

1 2 3 4

Sample Output First: 

1 3 2 4

Sample Input Second: 3 3

4 5 6 8 9 3 5 6 2

Sample Output Second: 

4 8 5 5 9 6 6 3 2


Data requirement:-


  Input Data:- row_size, col_size, matrix[][]


  Output Data:-tran_matrix[][]


  Additional Data:- i, j


Program in C

Here is the source code of the C Program to find the transpose of a matrix.


Code:


#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the matrix int i,j; printf("Enter the Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } //compute the transpose matrix int tran_matrix[col_size][row_size]; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { tran_matrix[i][j]=matrix[j][i]; } } //display the transpose Matrix printf("Transpose of the given Matrix is:\n"); for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { printf("%d ",tran_matrix[i][j]); } printf("\n"); } }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 1 2 3 4 Transpose of the given Matrix is: 1 3 2 4


Program in C++

Here is the source code of the C++ Program to find the transpose of a matrix.


Code:


#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the matrix int i,j; cout<<"Enter the Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } //compute the transpose matrix int tran_matrix[col_size][row_size]; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { tran_matrix[i][j]=matrix[j][i]; } } //display the transpose Matrix cout<<"Transpose of the given Matrix is:\n"; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { cout<<tran_matrix[i][j]<<" "; } cout<<"\n"; } }


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 4 5 6 8 9 3 5 6 2 Transpose of the given Matrix is: 4 8 5 5 9 6 6 3 2


Program in Java

Here is the source code of the Java Program to find the transpose of a matrix.


Code:


import java.util.Scanner; public class TrsnsposeOfMatrix { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the matrix int i,j; System.out.println("Enter the Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } //compute the transpose matrix int tran_matrix[][] =new int[row_size][col_size]; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { tran_matrix[i][j]=matrix[j][i]; } } //display transpose of the matrix System.out.println("Transpose of the Given Matrix is:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { System.out.print(tran_matrix[i][j]+" "); } System.out.println(); } cs.close(); } }


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 9 2 3 5 6 7 1 2 3 Transpose of the Given Matrix is: 9 5 1 2 6 2 3 7 3


Program in Python

Here is the source code of the Python Program to find the transpose of a matrix.


Code:


# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) # Compute transpose of two matrices tran_matrix=[[0 for i in range(col_size)] for i in range(row_size)] for i in range(0,row_size): for j in range(0,col_size): tran_matrix[i][j]=matrix[j][i] # display transpose of the matrix print("Transpose of the Given Matrix is:") for m in tran_matrix: print(m)


Input/Output:

Enter the Matrix Element:
4 5
6 7
Transpose of the Given Matrix is:
[4, 6]
[5, 7]



Most Recommend Questions:-








More Questions:-




Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question

Post a Comment

0 Comments