The Conversion of rows into columns and columns into rows is called as Transpose of a Matrix in C.

In this article we will show you the step by step approach involved in transpose of a Matrix in C Programming language with 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 and then we are going to convert rows into columns and columns into rows (also called as Transpose of a Matrix in C).

/* Transpose of a Matrix in c */ #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]); } } //Transpose of matrix 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; }

**OUTPUT**

**ANALYSIS:**

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

printf("\n Please Enter Number of rows and columns \n");

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

scanf("%d %d", &i, &j);

Below for loop will helps to iterate each and every cell present in the a[2][3] matrix. Conditions inside the for loops ((rows < i) and (columns < j)) will ensure the compiler, not to exceed the matrix limit. Otherwise the matrix will overflow

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]

for(rows = 0; rows < i; rows++) { for(columns = 0;columns < j; columns++) { scanf("%d", &a[rows][columns]); } }

In the next line, We have one more for loop.

for(rows = 0; rows < i; rows++) { for(columns = 0;columns < j; columns++) { b[columns][rows] = a[rows][columns]; } }

Above For loop is used to Transpose of a Matrix a[2][3] and placing in b. From the above screenshot

User inserted values for transpose of a matrix in C example are

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

**Row First Iteration**

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

**Column First Iteration**

The value of column will be 0 and the condition (columns < 2) is True. So, it will start executing the statements inside the loop until the condition fails.

b[columns][rows] = a[rows][columns];

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

b[0][0] = 15

**Column Second Iteration**

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

b[columns][rows] = a[rows][columns];

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

b[1][0] = 25

**Column 3**^{rd}**Iteration**

The value of columns will be 2 and the condition (columns < 3) is True. Since we didn’t exit from the inner loop (Columns loop), Row value will be 0

b[columns][rows] = a[rows][columns];

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

b[2][0] = 35

After the increment the value of columns 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 and starts the second row iteration

**Row Second Iteration**

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

**Column First Iteration**

The value of column will be 0 and the condition (columns < 3) is True. So, it will start executing the statements inside the loop until the condition fails.

b[columns][rows] = a[rows][columns];

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

b[0][1] = 45

**Column Second Iteration**

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

b[columns][rows] = a[rows][columns];

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

b[1][1] = 55

**Column 3**^{rd}**Iteration**

The value of columns will be 2 and the condition (columns < 3) is True. Since we didn’t exit from the inner loop (Columns loop), Row value will be 0

b[columns][rows] = a[rows][columns];

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

b[2][1] = 65

After the increment the value of columns 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 rows =2. Condition (rows < 2) will fail So, it will exit from the loop.

After transposing the matrix a[2][3] will become b[3][2]

Next for loops, Will traverse as we explained above but instead of transposing it will display the values one by one using the printf statements inside them.

for(rows = 0; rows < j; rows++) { for(columns = 0; columns < i; columns++) { printf("%d \t ", b[rows][columns]); } printf("\n"); }

If you observe the above for loop in this C transpose of a matrix program. We assigned the rows to j and columns to I because, initially user entered values 2 rows and 3 columns. After transposing the matrix it became 3 rows and 2 columns, that’s why we assigned j value to rows and i value to columns. If it is not dynamic program then you can place the constant values directly in the place of i and j.

The final output of the Addition Array is:

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

Thank you for visiting our Blog