Transpose of a Matrix in C Program


Program Description:

Write a Program to Calculate the Transpose of a Matrix in C programming language. The program should accept a Matrix from the user and calculate and display the transpose of the input matrix.

Before going any further let’s look at what is the Transpose of Matrix.

Transpose of Matrix:

The Transpose of a Matrix can be created by interchanging the original matrix rows into the columns and columns into the rows. (Exchange the Rows with Columns of original Array)

For Example, We have Matrix X with the m rows and n columns. Then Transpose of the matrix X contains the n rows and m columns Such that the i-th column of Orignal Matrix is the i-th row of Trasnpose Matrix.

Transpose of Matrix X can be denoted by XT

If X is an m x n matrix, then its transpose XT is an n x m matrix. The elements of XT are obtained by reflecting the elements of X across the main diagonal

Matrix Transpose Example:

Here is the example.

Original Matrix X:

Transpose of X ( XT):

Here is the graphical representation of the Transpose of a Matrix


Program Prerequisites:

It is recommended to know the basics of the C Arrays, Functions in C, and Mutli-dimentional Arrays or Matrices in C langauge.

Transpose of a Matrix in C Program Explanation/Algorithm:

Let’s look at the step by step explanation of the Matrix Transpose Program in C.

  1. Create two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns in the Matrix. Please change this number if you want to play with the large arrays.
  2. Start the Program by declaring two Matrices (2D Arrays). They are X matrix, and XTranspose Matrix. The row size and column size of the matrices are ROWS and COLUMNS constants respectively.
  3. Prompt the user to provide the desired Row size and Column Size, and store them in the variables rows and columns respectively. (Note, These rows and columns are small letters – C is Case Sensitive language)
  4. Check for array bounds using the (rows >= ROWS || rows <= 0 || columns >= COLUMNS || columns <= 0) condition, And display an error message if the row/column sizeis beyond the present limits.
  5. Take the user input for Input Matrix i.e Matrix-X, We need to use two For Loops, One For Loop will iterate over the rows, and the Second For Loop will iterate over the columns
  6. Create the First For Loop ( Outer Loop), Start the iteration from i=0 and go till the i<rows (that is for(i=0; i<rows; i++) ). At Each iteration of the Outer Loop,
    • Create the second For Loop ( Inner Loop). This loop is a Nested loop that will be inside the Outer Loop. Start the Loop from j=0 and go till the j<columns (i.e for(j=0; j<columns; j++) ). The Inner Loop will iterate over the columns. At each iteration of the Inner Loop
      • Prompt the user to provide the Matrix element and Read the element using the scanf function and Update the X[i][j] element.
      • Repeat the above step for all elements in the Matrix-X
  7. Now, To Calculate the Transpose of the X, We need to use two more for loops.
  8. Create Outer For Loop ( Iterates over rows) – for(i=0; i<rows; i++)
    • Then Create the Inner For loop (Iterates over columns) – for(j=0; j<columns; j++)
      • Copy the value of the X[i][j] element to the XTranspose[j][i]. Note that, i and j are interchanged.
      • i.e XTranspose[j][i] = X[i][j];
  9. Once the step-8 is complted, The XTranspose Matrix contains the Transpose of the X (Matrix).
  10. Display the XTranspose Matrix on the console. ( We need to use two for loops)

📢This program is part of the Array Practice Programs Series.

Program to Calculate the Transpose of a Matrix in C Language:

Here is the program to Calculate the Transpose of the Matrix in C Programming language.

Program Output:

Compile the Program using GCC (Any compiler)

$ gcc transpose.c

Run the Program. ( ./a.out)

Test Case 1: When the Matrix Rows and Columns are of Same size:


As we can see from the above output, The program is providing the desired results.

Test Case 2: When the Matrix Row size is not equal to Column Size:

As excepted, When the user provided the 3X2 matrix(X), The program calculated the Transpose of the Matrix(XT), which is of 2X3 size.

Test Case 3: When the size is out of limits:

The program displays an error messge Invalid Rows or Columns size, Please Try again when the size goes out of the limits .

Transpose of a Matrix using Functions in C Programming:

Let’s rewrite the above program to use the user-defined functions for reading the matrix, printing the matrix and calcluating the transpose of matrix.

Here is the Transpose of Matrix with functions.

We have defined three functions in the above program (except main() function)

  1. The readMatrix() function – Used to read the elements from the user and update the Matrix X
    • Function Prototypevoid readMatrix(int rows, int columns, int X[rows][columns])
    • The readMatrix() function takes three formal arguments which are rows, columns, and Matrix X.
    • This function uses Two For loops to iterate over the given matrix A and updates its elements by using the scanf function – scanf(“%d”, &X[i][j]);
    • The readMatrix() function doesn’t return anything back to the caller. So return type is void.
    • As the arrays are passed by the address/reference, All the changes made to the Matrix-X in the readMatrix() function will be visible in the Caller Function (i.e main() function).
  2. The displayMatrix() function is used to print the Matrix elements on the Standard Output.
    • The prototype of displayMatrix function is void displayMatrix(int rows, int columns, int X[rows][columns])
    • Similar to the readMatrix() function, The displayMatrix() function also takes three formal arguments. i.e rows, columns, and X[][] (2D Array)
    • The displayMatrix() function uses Two For loops to iterate over the X matrix and print the elements of the matrix. – printf("%d ", X[i][j]);
  3. The calculateTranspose() function – This function used to calculate the Transpose of the given matrix
    • Function Prototypevoid calculateTranspose(int rows, int columns, int X[rows][columns], int XTranspose[columns][rows])
    • As you can see from the above prototype, This function takes four Formal Arguments which are rows, columns, matrix X, matrix XTranspose.
    • The addMatrices() function uses two for loops to iterate over the elements of the Matrices X and calculates the Transpose of X by interchanging the matrix elements – XTranspose[j][i] = X[i][j];

Program Output:

Let’s compile and Run the program and observ the output

Let’s try a few more examples.


As you can see from the above output, The program is generating the desired results.

For example, When user provided the 1X5 Input matrix 1000 2000 3000 4000 5000, The program generated the 5X1 transpose matrix.

Related Programs:


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

6 Responses

  1. […] have looked at the Matrix Transpose Program in earlier articles, In today’s article, We will look at the Multiplication of Two Matrices in C […]

  2. […] C Program to Calculate the Transpose of Matrix […]

  3. […] C Program to Calculate the Transpose of Matrix […]

  4. […] C Program to Calculate the Transpose of Matrix […]

  5. […] 1: Calculate the Transpose of the Matrix […]

  6. […] C Program to Calculate the Transpose of Matrix […]

Leave a Reply