Transpose of a Matrix in C

What is Transpose of a Matrix?

The transpose of a matrix is found by interchanging its rows into columns or columns into rows. The transpose of the matrix is denoted by using the letter “T” in the superscript of the given matrix.

For example, if “A” is the given matrix, then the transpose of the matrix is represented by A’ or AT.

Example 1:
If the given matrix \( A =
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9
\end{bmatrix}\), then the transpose matrix (AT) is \( \begin{bmatrix}
1 & 4 & 7\\
2 & 5 & 8\\
3 & 6 & 9
\end{bmatrix}\)

Since it is a square matrix, the order of the matrix does not change when it is transposed.

Example 2:
If the order of the original matrix is 2×3, the order of the transposed matrix will be 3×2, as shown below.

If the given matrix \( A =
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6
\end{bmatrix}\), then the transpose matrix (AT) is \( \begin{bmatrix}
1 & 4 \\
2 & 5\\
3 & 6
\end{bmatrix}\)

Problem Description

Write a C Program to find the transpose of a given matrix.

advertisement
advertisement
Problem Solution

The transpose of a given matrix is calculated by interchanging the rows and columns of a matrix.

There are two ways to find the transpose of a matrix in C.

Method 1: C Program to Find Transpose of a Matrix using Loops

In this approach, we iterate over the matrix using two loops and store matrix[i][j] in its transpose[j][i]. Basically we are interchanging rows with columns. The Element at index i,j will now be stored at j,i.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Algorithm:

1. Start the program.
2. Input the order of matrix.
3. Input the elements of the matrix.
4. Store transpose of matrix in a transpose matrix by iterating over complete matrix.
5. End the Program.

Program/Source Code

Here is source code of the C program to find the transpose of a given matrix using loops (For Loop). The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Find Transpose of a Matrix using Loops
 */
 
#include <stdio.h>
 
void main()
{
    static int array[10][10],transpose[10][10];
    int i, j, m, n;
 
    printf("Enter the order of the matrix \n");
    scanf("%d %d", &m, &n);
    printf("Enter the coefficients of the matrix\n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            scanf("%d", &array[i][j]);
        }
    }
    printf("The given matrix is \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            printf(" %d", array[i][j]);
        }
        printf("\n");
    }
    for (i = 0;i < m;i++)
    {
        for (j = 0; j < n; j++)
        {   
            transpose[j][i] = array[i][j];
        }
    }
 
    // printing the transpose of a matrix
    printf("Transpose of matrix is \n");
    for (i = 0; i< n; i++)
    {
        for (j = 0; j < m; j++)
        {
           printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }
}
Program Explanation

1. First input the order of the matrix, then take input of elements of the matrix.
2. In this C program, we are reading the order of the matrix row and column using ‘m’ and ‘n’ variables respectively.
3. Using for loop the coefficient elements of the matrix is assigned to the variable array[i][j].
4. Then find out the transpose of the matrix by storing the value of array[i][j] in transpose[j][i] i.e storing values of rows in columns.
5. Then, print the transpose of the matrix.

advertisement

Time Complexity: O(m*n)
In the program, we are iterating over the complete matrix, so time complexity is O(m*n), where m, n are the rows and columns of the matrix.

Space Complexity: O(m*n)
Space is required to store the transpose of matrix, which is O(m*n), where m, n are the rows and columns of the matrix.

Runtime Test Cases

In this case, we enter the order of the matrix as “3 * 3” and the matrix elements as input to find the transpose of a matrix.

Enter the order of the matrix 
3 3
Enter the coefficients of the matrix
1 2 3
4 5 6
7 8 9
The given matrix is 
1 2 3
4 5 6
7 8 9
Transpose of matrix is 
1 4 7
2 5 8
3 6 9

advertisement
Method 2: Transpose of a Matrix in C using Loops and Function

In this approach, we use a function to transpose a matrix. In the function, we traverse the whole matrix and store the value at index i,j in the index j,i of the transpose matrix. Afterwards we print the transpose matrix.

Algorithm to Find Transpose of a Matrix using Loop and Function:
1. Start the program.
2. Input the order of matrix.
3. Input the elements of the matrix.
4. Declare a function to find transpose of the matrix.
5. Call the function.
6. End the Program.

Program/Source Code

Here is source code of the C program to find the transpose of a given matrix using loops and function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Find Transpose of a Matrix using Loops and Function
 */
 
#include <stdio.h>
 
void transpose_matrix(int m,int n, int array[10][10])
{
    //transposing array matrix
    int transpose[10][10];
    for (int i = 0;i < m;i++)
    {
        for (int j = 0; j < n; j++)
        {   
            transpose[j][i] = array[i][j];
        }
    }  
    printf("Transpose of matrix is \n");
    for (int i = 0; i< n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }
}
 
void main()
{
    static int array[10][10];
    int i, j, m, n;
 
    printf("Enter the order of the matrix \n");
    scanf("%d %d", &m, &n);
    printf("Enter the coefficients of the matrix\n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            scanf("%d", &array[i][j]);
        }
    }
    printf("The given matrix is \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            printf("%d ", array[i][j]);
        }
        printf("\n");
    }
    transpose_matrix(m,n,array);
}
Program Explanation

1. First input the order of the matrix, then take input of elements of the matrix.
2. Then declare a function to find out the transpose of the matrix by storing the value of array[i][j] in transpose[j][i] i.e storing values of rows in columns.
3. Then, execute or call the function.
4. Print the transpose of the matrix.

Time Complexity: O(m*n)
In the program, we are iterating over the complete matrix, so time complexity is O(m*n), where m, n are the rows and columns of the matrix.

Space Complexity: O(m*n)
Space is required to store the transpose of matrix, which is O(m*n), where m, n are the rows and columns of the matrix.

Runtime Test Cases

In this case, we enter the order of the matrix as “2 * 3” and the matrix elements as input to find the transpose of a matrix.

Enter the order of the matrix 
2 3
Enter the coefficients of the matrix
1 2 3
4 5 6
The given matrix is 
1 2 3 
4 5 6 
Transpose of matrix is 
1 4 
2 5 
3 6

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.