codescracker
c

C Program to Find Transpose of a Matrix



« Previous Program Next Program »

In this article, you will learn and get code on finding the transpose of given matrix by user at run-time using a C program. But before starting the program, let's first understand, how to find the transpose of any matrix.

How to Find Transpose of a Matrix ?

Transpose of a matrix can be calculated by switching the rows with columns or columns with rows. For example, if a given matrix say Matrix A is:

1  1  1
2  2  2
3  3  3

Then, its transpose say AT will be:

1  2  3
1  2  3
1  2  3

As you can see that all the three rows gets switched to three columns in a way that:

Find Transpose of a Matrix in C

This program is created for 3*3 matrix. That is a matrix with 3 rows and 3 columns. Therefore, this program asks from user to enter 9 elements for the matrix, and then will find its transpose:

// C Program to Find Transpose of a Matrix
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int mat[3][3], i, j, matTrans[3][3];
    printf("Enter 3*3 Matrix Elements: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            scanf("%d", &mat[i][j]);
    }
    // Transposing the Matrix...
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            matTrans[j][i] = mat[i][j];
    }
    printf("\nTranspose of given Matrix is:\n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            printf("%d  ", matTrans[i][j]);
        printf("\n");
    }
    getch();
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

c program transpose matrix

Now enter any 9 elements for the matrix and press ENTER key to see the following output:

matrix transpose c

The following block of code is responsible for transposing the matrix:

for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
        matTrans[j][i] = mat[i][j];
}

If you want to learn about for loop in detail, then refer to the separate tutorial on it. But for now, the above code block executes in a way that, the initialization part say i=0 executes only once at start of the loop, and every time while entering the block of code inside the loop, its condition (second statement of loop) must be checked. If the ocndition evaluates to be true, then only the program flow goes inside the body of loop and executes all the statements

Let's suppose, user enters 9 elements as 1, 1, 1, 2, 2, 2, 3, 3, 3. Therefore mat[0][0]=1, mat[0][1]=1, mat[0][2]=1, mat[1][0]=2, mat[1][1]=2 and so on. Because, i starts from 0 and executes until its value becomes 3, therefore loop executes total of 3 times. That is, both the loop executes 3 times in a way that:

In this way, we will have a matrix named matTrans[][] that has its value as:

which is the transpose of given matrix A.

Allow User to Define Matrix Dimension

This program allows the user to define the dimension of orignal matrix. That is, user has to enter the size of row and column for the matrix. And also enter the matrix element of given size. Then will calculate and print the transpose:

// Transpose of Matrix with Different Number of Rows and Columns
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int mat[10][10], rowSize, colSize, i, j, matTrans[10][10];
    printf("Enter the Size of Row and Column: ");
    scanf("%d%d", &rowSize, &colSize);
    printf("\nEnter %d*%d Matrix Elements: ", rowSize, colSize);
    for(i=0; i<rowSize; i++)
    {
        for(j=0; j<colSize; j++)
            scanf("%d", &mat[i][j]);
    }
    printf("\nOriginal Matrix is:\n");
    for(i=0; i<rowSize; i++)
    {
        for(j=0; j<colSize; j++)
            printf("%d  ", mat[i][j]);
        printf("\n");
    }
    // Transposing the Matrix.....
    for(i=0; i<rowSize; i++)
    {
        for(j=0; j<colSize; j++)
            matTrans[j][i] = mat[i][j];
    }
    printf("\nTranspose of Matrix is:\n");
    for(i=0; i<colSize; i++)
    {
        for(j=0; j<rowSize; j++)
            printf("%d  ", matTrans[i][j]);
        printf("\n");
    }
    getch();
    return 0;
}

Here is it sample run:

transpose of a matrix c

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker