The Conversion of rows into columns or columns into rows is called the C Transpose of a Matrix. How to write a program to transpose of a Matrix in C Programming language with an example.

## Transpose of a Matrix in C Programming Example

This transpose of a matrix in C program allows the user to enter the number of rows and columns of a Two Dimensional Array. Then the C program is going to convert rows into columns and vice versa, also called the Transpose of a Matrix.

#include<stdio.h> int main() { int i, j, rows, columns, a[10][10], b[10][10]; printf("\nPlease Enter Number of rows and columns\n"); scanf("%d %d", &i, &j); printf("\n Please Enter the Array Elements \n"); for(rows = 0; rows < i; rows++) { for(columns = 0;columns < j; columns++) { scanf("%d", &a[rows][columns]); } } for(rows = 0; rows < i; rows++) { for(columns = 0;columns < j; columns++) { b[columns][rows] = a[rows][columns]; } } printf("\n Transpose Matrix Elemnts are: \n"); for(rows = 0; rows < j; rows++) { for(columns = 0; columns < i; columns++) { printf("%d \t ", b[rows][columns]); } printf("\n"); } return 0; }

In this C Program to transpose a matrix, We declared 2 Two dimensional arrays, a and b, with the size of 10 * 10. The first printf statement asks the User to enter the Two Dimensional Array a, b size (Number of rows and columns. For instance, 2 Rows, 3 Columns = a[2][3])

The C Programming scanf statement will assign the user entered values to i and j (Rows = i and Columns = j).

The first for loop in this program will help to iterate each cell present in a[2][3] matrix. Conditions inside the for loops ((rows < i) and (columns < j)) will ensure the compiler does not exceed the limit. Otherwise, it will overflow.

The scanf statement inside the for loop will store the user entered values in every individual array element such as a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]

In the next line, We have one more for loop. And the above For loop is used to Transpose a Matrix a[2][3] and place in b.

From the above screenshot, the user inserted values for C transpose of a matrix example are

a[2][3] = { {15, 25, 35}, { 45, 55, 65} }

Row First Iteration

The value of the row will be 0, and the condition (0 < 2) is True. So, it will enter into the second for loop.

C Transpose of a Matrix Column First Iteration

The value of the column will be 0, and the condition (0 < 2) is True.

b[columns][rows] = a[rows][columns] => a[0][0]

b[0][0] = 15

Col Second Iteration

columns = 1, and the condition (1 < 3) is True. Since we didn’t exit from the inner loop (Cols loop), the Row value will be 0

b[1][0] = a[0][1]

b[1][0] = 25

Column 3^{rd} Iteration

col = 2, and the condition (2 < 3) is True.

b[2][0] = a[0][2]

b[2][0] = 35

After the increment, the value of the column will be 3, and the condition (3 < 3) will fail. So it will exit from the loop.

Now the value of rows will be incremented and starts the second iteration of Transpose of a Matrix in C.

Row Second Iteration

The value of the row will be 1, and the condition (1 < 2) is True. So, it will enter into the second for loop.

Column First Iteration

The value of the column will be 0, and the condition (0 < 3) is True.

b[0][1] = a[1][0]

b[0][1] = 45

Col Second Iteration

column = 1, and the condition (1 < 3) is True. We are still in the inner loop, so the Row value is 1

b[1][1] = a[1][1]

b[1][1] = 55

Transpose of a matrix in C Col 3^{rd} Iteration

cols = 2, and the condition (2 < 3) is True.

b[2][1] = a[1][2]

b[2][1] = 65

After the increment, the value of cols will be 3, and the condition (columns < 3) will fail. So it will exit from the loop.

Now the value of rows will be incremented it means 2, and the condition (2 < 2) will fail. So, it will exit from the loop.

After transposing of a matrix in C, a[2][3] will become b[3][2]

Next for loops will traverse, as we explained above. Still, instead of transposing, it displays the values one by one using the printf statement.

If you observe the above for loop in this example, we assigned the rows to j and col to i. Because initially, user-entered values 2 rows and 3 cols. After transposing the matrix in C, it became 3 rows and 2 columns. That’s why we assigned the j value to rows and i value to columns. If it is not a dynamic program, place the constant values directly in the place of i and j.

The final output of the is: b[3][2] = { {15, 45}, {25, 55}, {35, 65} };