Signup/Sign In
PUBLISHED ON: APRIL 30, 2021

C Program To Find the Transpose of a Matrix

The transpose of a matrix is obtained by changing the rows to column and column to rows. Consider there is an M*N matrix, where M stands for the number of rows and N stands for the number of columns. Now, the new transpose matrix obtained will be of form N*M where N stands for the number of rows and M stands for the number of columns.

But before moving forward, if you are not familiar with the concept of array then, do check the article on Arrays in C.

This problem can be solved in the following ways:

Method 1: Without using Functions

Method 2: Using Functions

Let us look at each of the methods separately.

Program 1: Find Transpose of a Matrix

In this method, firstly we will declare two 2D arrays. The first 2D array will contain the original matrix and the second 2d array will contain the new transpose matrix. Here, we will directly transpose the elements of the original matrix to the new matrix.

Algorithm:

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Declare a transpose matrix.
  5. Store the elements in the transpose matrix.
  6. Now, print the elements in the transpose matrix.
  7. Stop

Below is the code for the same.

In the below program, we will directly find the transpose of the matrix.

#include <stdio.h>

int main()
{
    int m,n;                 //Matrix Size Declaration
    printf("Enter the number of rows and column: \n");
    scanf("%d %d",&m,&n);   //Matrix Size Initialization
    int arr[10][10];        //Matrix Size Declaration
    printf("\nEnter the elements of the matrix: \n");
    for(int i=0;i<m;i++)    //Matrix Initialization
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    printf("\nThe elements in the matrix are: \n");
    for(int i=0;i<m;i++)     //Print the matrix
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
    int brr[10][10];         //Transpose Matrix Declaration
    for(int i=0;i<m;i++)     //Transpose Matrix initialization
    {
        for(int j=0;j<n;j++)
        {
            brr[j][i]=arr[i][j];     //Store elements in the transpose matrix
        }
    }
    printf("\nAfter transpose the elements are...\n");
    for(int i=0;i<m;i++)      //Print the transpose matrix
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",brr[i][j]);
        }
        printf("\n");
    }
    return 0;
}


Enter the number of rows and column: 3 3

Enter the elements of the matrix: 1 4 3 5 6 7 2 9 8

The elements in the matrix are:
1 4 3
5 6 7
2 9 8

After transpose the elements are...
1 5 2
4 6 9
3 7 8

Program 2: Find the Transpose of a Matrix

In this method, firstly we will declare two 2D array. The first 2D array will contain the original matrix and the second 2d array will contain the new transpose matrix. Here, we will call a function that will transpose the elements of the original matrix to the new matrix.

Algorithm:

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Declare a transpose matrix.
  5. Call a function that will perform the transpose operation.
  6. Store the elements in the transpose matrix.
  7. Now, print the elements in the transpose matrix.
  8. Stop

Below is the code for the same.

In the below program, we will call a function to find the transpose of the matrix.

#include <stdio.h>
void transpose(int arr[10][10], int m, int n, int brr[10][10])     //Function Definition
{
    for(int i=0;i<m;i++)     //Transpose Matrix initialization
    {
        for(int j=0;j<n;j++)
        {
            brr[j][i]=arr[i][j];     //Store elements in the transpose matrix
        }
    }
    printf("\nAfter transpose the elements are...\n");
    for(int i=0;i<m;i++)      //Print the transpose matrix
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",brr[i][j]);
        }
        printf("\n");
    }
}
int main()
{
    int m,n;                 //Matrix Size Declaration
    printf("Enter the number of rows and column: \n");
    scanf("%d %d",&m,&n);   //Matrix Size Initialization
    int arr[10][10];        //Matrix Size Declaration
    printf("\nEnter the elements of the matrix: \n");
    for(int i=0;i<m;i++)    //Matrix Initialization
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    printf("\nThe elements in the matrix are: \n");
    for(int i=0;i<m;i++)     //Print the matrix
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
    int brr[10][10];         //Transpose Matrix Declaration
    transpose(arr,m,n,brr);  //Function Call
    return 0;
}


Enter the number of rows and column: 3 3

Enter the elements of the matrix: 1 4 9 7 8 5 2 9 8

The elements in the matrix are:
1 4 9
7 8 5
2 9 8

After transpose the elements are...
1 7 2
4 8 9
9 5 8



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight