The Conversion of rows into columns, or columns into rows is called Transpose of a Matrix in C. How to write a program to 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. Then we are going to convert rows into columns and columns into rows (also called 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. The first 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 Transpose of a Matrix in C.
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 (0 < 2) is True. So, it will enter into second for loop
Column First Iteration
The value of column will be 0, and the condition (0 < 2) is True.
b[columns][rows] = a[rows][columns];
b[0][0] = a[0][0]
b[0][0] = 15
Column Second Iteration
columns = 1, and the condition (1 < 3) is True. Since we didn’t exit from the inner loop (Columns loop), Row value will be 0
b[1][0] = a[0][1]
b[1][0] = 25
Column 3rd Iteration
columns = 2, and the condition (2 < 3) is True.
b[2][0] = a[0][2]
b[2][0] = 35
After the increment, the columns value 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 row iteration
Row Second Iteration
The value of row will be 1, and the condition (1 < 2) is True. So, it will enter into second for loop (columns loop)
Column First Iteration
The value of column will be 0, and the condition (0 < 3) is True.
b[0][1] = a[1][0]
b[0][1] = 45
Column Second Iteration
column = 1, and the condition (1 < 3) is True. We are still in the inner loop (Columns loop), so Row value is 1
b[1][1] = a[1][1]
b[1][1] = 55
Column 3rd Iteration
columns = 2, and the condition (2 < 3) is True.
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, and the condition (2 < 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. Still, instead of transposing, it displays the values one by one using the printf statement.
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 in C, 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 a dynamic program, place the constant values directly in the place of i and j.
The final output of the Transpose of a Matrix in C is:
b[3][2] = { {15, 45}, {25, 55}, {35, 65} };