Program to find the transpose of a matrix in C, C++, Java and Python | faceprep

Program to find the transpose of a matrix is discussed here. Transpose of a matrix can be performed by exchanging the elements of row by column and the elements of a column by row. For example:

Input:

1   2   3
4   5   6
7   8   9

output:

1   4   7
2   5   8
3   6   9

Algorithm

transpose of a mtraix

Consider the matrix:

m[0][0] = 10
m[0][1] = 20
m[1][0] = 30
m[1][1] = 40
m[2][0] = 50
m[2][1] = 60

The transpose of the above matrix can be done by this way:

r_m[0][0] = m[0][0] = 10
r_m[0][1] = m[1][0] = 30
r_m[0][2] = m[2][0] = 50
r_m[1][0] = m[0][1] = 20
r_m[1][1] = m[1][1] = 40
r_m[1][2] = m[2][1] = 60

Program to find the transpose of a matrix

#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
scanf(“%d%d”, &m, &n);
for (c = 0; c < m; c++)
for(d = 0; d < n; d++)
scanf(“%d”, &matrix[c][d]);
for (c = 0; c < m; c++)
for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
printf(“%d\t”, transpose[c][d]);
printf(“\n”);
}
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
cin >> m >> n;
for (c = 0; c < m; c++)
for(d = 0; d < n; d++)
cin >> matrix[c][d];
for (c = 0; c < m; c++)
for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
cout << transpose[c][d] << ” “;
cout << “\n”;
}
return 0;
}

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int i, j;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number of rows: “);
int row = sc.nextInt();
System.out.println(“Enter number of columns: “);
int column = sc.nextInt();
int matrix[][] = new int[row][column];
System.out.println(“Enter matrix:”);
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
matrix[i][j] = sc.nextInt();
System.out.print(” “);
}
}

System.out.println(“The Input Matrix is “);
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(matrix[i][j]+” “);
}
System.out.println(” “);
}
System.out.println(“The above matrix after Transpose is “);
for(i = 0; i < column; i++)
{
for(j = 0; j < row; j++)
{
System.out.print(matrix[j][i]+” “);
}
System.out.println(” “);
}
}
}

matrix=[]
row=int(input(“Enter number of rows: “))
column=int(input(“Enter number of columns: “))
for i in range(row):
matrix.append([])
for j in range(column):
num=int(input(“Enter the element: “))
matrix[i].append(num)
print(‘\n’)
print(“Input matrix: “)
for i in range (row):
for j in range (column):
print (matrix[i][j], end=” “)
print(‘\n’)

transpose=[]
for j in range(column):
transpose.append([])
for i in range (row):
t_num=matrix[i][j]
transpose[j].append(t_num)

print(‘\n’)
print(“Transpose matrix: “)
for i in range (row):
for j in range (column):
print (transpose[i][j], end=” “)
print(‘\n’)

#include<stdio.h>
#include<stdlib.h>
int i,j;
int main()
{
int **a,**b,n;
scanf(“%d”,&n);

a=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
*(a+i)=(int*)malloc(n*sizeof(int));
b=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
*(b+i)=(int*)malloc(n*sizeof(int));

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,*(a+i)+j);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
*(*(b+j)+i)=*(*(a+i)+j);
}
}
printf(“Given square matrix is:\n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(“%d “,*(*(a+i)+j));
printf(“\n”);
}
}

printf(“Transpose matrix is:\n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(“%d “,*(*(b+i)+j));
printf(“\n”);
}
}
return 0;
}

Output:

transpose of a matrix output