Program to Transpose a matrix

Description

The transpose of a matrix is a new matrix whose rows are the columns of the original.
If matrix having dimension m*n the it's transpose having dimension will be n*m.

C/C++

/* C program to transpose a matrix */
//Save it as TransposeMatrix.c

#include<stdio.h>
int main(){

    int i, j, m, n;

    printf("Enter the number of rows of array : ");
    scanf("%d",&m);

    printf("Enter the number of columns of array : ");
    scanf("%d",&n);

    //Declaring 2D array
    int arr[m][n];

    //Reading input
    for(i=0;i<m;i++) {
        for(j=0;j<n;j++) {
            printf("arr[%d][%d] : ",i,j);
            scanf("%d",&arr[i][j]);
        }
    }

    //Displaying Output
    printf("The entered array : \n");
    for(i=0;i<m;i++) {
        for(j=0;j<n;j++) {
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }

    //Transpose of matrix
    printf("The transpose of the matrix : ");
    for(j=0;j<n;j++) {
        printf("\n");
        for(i=0;i<m;i++) {
            printf("%d ",arr[i][j]);
        }
    }

    return 0;
}
Input:
Enter the number of rows of array : 2
Enter the number of columns of array : 3
arr[0][0] : 1
arr[0][1] : 2
arr[0][2] : 3
arr[1][0] : 4
arr[1][1] : 5
arr[1][2] : 6

Output:
The entered array :
1 2 3
4 5 6
The transpose of the matrix :
1 4
2 5
3 6

Java

/* Java program to transpose a matrix */
//Save it as TransposeMatrix.java


import java.io.*;
import java.util.Scanner;

public class TransposeMatrix {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int i, j, m, n;

        System.out.println("Enter the number of rows of array : ");
        m = scanner.nextInt();

        System.out.println("Enter the number of columns of array : ");
        n = scanner.nextInt();

                //Declaring 2D array
        int arr[][] = new int[m][n];

                //Reading input
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                System.out.println("arr[" + i + "]" + "[" + j + "] : ");
                arr[i][j] = scanner.nextInt();
            }
        }

                //Displaying Output
        System.out.println("The entered matrix : ");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        
        Transpose of matrix
        System.out.println("The transpose of the matrix : ");
        for(j=0;j<n;j++) {
            for(i=0;i<m;i++) {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }
}
Input:
Enter the number of rows of array : 
2
Enter the number of columns of array : 
3
arr[0][0] : 
1
arr[0][1] : 
2
arr[0][2] : 
3
arr[1][0] : 
4
arr[1][1] : 
5
arr[1][2] : 
6

Output:
The entered matrix : 
1 2 3 
4 5 6 
The transpose of the matrix : 
1 4 
2 5 
3 6

Related Programs

1) Program to read and display 2D array
2) Program to Add two Matrices
3) Program to Multiply two Matrices
4) Program to Find the smallest missing number
5) Program to Search for an Element in an Array
6) Program to Rearrange an array such that arr[i]=i
7) Program to Find missing odd number in first n odd number
8) Program to find maximum and second maximum if elements of array is space separated input
9) Program to Print How Many Numbers Smaller than Current Number
10) Remove Duplicate Elements From Array
Share Me

Leave a Reply