C Program to Transpose a Matrix


A matrix is defined as a rectangular arrangement of numbers in the form of rows and columns. When the elements in rows and columns of a matrix are exchanged, a new matrix is formed which is known as transpose of the matrix. It carries a great significance in structural dynamics calculation, electrical engineering, mathematics and physics.

Finding the transpose of a matrix in C is a popular tutorial under “array”. It basically gives the idea of matrix inputting, manipulating and outputting using the standard input/output functions of the C language.

The basic logic behind matrix transposition is swapping the elements of row and respective column. After entering all the elements of the matrix, the number of rows and columns are swapped and a loop is started to exchange the elements. The exchanged or newly formed elements are stored as the elements of transposed matrix.

Algorithm to Transpose a Matrix:

  • Start
  • Declare all the necessary variables
  • Enter the order of matrix
  • Enter the elements of matrix row-wise using loop
  • Display the entered matrix in standard format (it is not a compulsory step)
  • Assign number of rows with number of column
  • Swap (i, j)th element with (j, i)th
  • Store the new elements as element of transposed matrix
  • Print the elements of transpose matrix in format using loop
  • Stop

Source Code in C to Transpose a Matrix:

When this C program is executed, it asks for the number of rows and columns of the matrix to be transposed. Then, the elements of matrix need to be entered row wise. Finally, the transposed matrix is displayed as console output in standard matrix format following the algorithm aforementioned.

Transposing a Matrix in C Program Output

There are other various ways of solving matrix transposition problem in C, such as by using function, pointers, etc. The presented source code is to be compiled in Code::Blocks IDE. If you have any questions regarding the source code or algorithm here, bring them up from the comments section.


Please enter your comment!
Please enter your name here