Transpose of a Matrix in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

A transpose of a matrix is obtained by interchanging rows of the original matrix with columns and vice-versa.

It is very commonly used in mathematics as well as programming while multiplying two matrices.

Before reading this article, understand the following C Programming topics:

Scope

  • We will understand what is the transpose of a matrix in detail.
  • In this article, we will look at what is a transpose of a matrix and how we can find the transpose of a given matrix by following a simple algorithm.
  • Later on, we’ll cover how to write a C program to find the transpose of a matrix.

What is the Transpose of a Matrix in C?

A transpose of a matrix is obtained by interchanging rows of the original matrix with columns and vice-versa. This could be imagined as if there are two matrices P and Q, such that rows of matrix Q are the columns of matrix P and columns of matrix Q are rows of matrix P, then it is said that the matrix Q is the transpose of matrix P.

This operation can be simply represented as follows:

where i and j represent the rows and columns respectively.

Example –

transpose of a matrix in c

In this example, the order of the matrix remains the same since it is a square matrix. But, if the order of the original matrix is, say 2×3 then the order of the transposed matrix would be 3×2, as shown in the figure below.

transpose of a matrix in c

Now, before moving on to the program for finding the transpose of a matrix in C, let us first look at an algorithm to understand the approach to be followed.

Algorithm for Finding Transpose of a Matrix in C

  1. Declare and initialize a 2-D array p[a][b] of order axb.
  2. Read the matrix p[a][b] from the user.
  3. Declare another 2-dimensional array t to store the transpose of the matrix. This array will have the reversed dimensions as of the original matrix.
  4. The next step is to loop through the original array and convert its rows to the columns of matrix t.
    • Declare 2 variables i and j.
    • Set both i,j=0
    • Repeat until i<b
      • Repeat until j<a
      • t[i][j] = p[j][i]
      • j=j+1**
    • i=i+1
  5. The last step is to display the elements of the transposed matrix t.

C Program to find the Transpose of a Matrix

Output –

The matrix used in the program is the same as that mentioned in the example at the beginning of the article.

Conclusion

  • In this article, we learnt about the transpose of a matrix and wrote a program to find the transpose of a matrix using C.