In this post, we will learn how to find the transpose of a matrix using C Programming language.
The Transpose of a matrix is obtained by interchanging rows and columns of the given matrix.
Suppose, we have a matrix A such that:
A = 1 2 3
4 5 6
Then, it’s transpose will be
A' = 1 4
2 5
3 6
We will be calculating the transpose of the given matrix using for loop.
So, without further ado, let’s begin this tutorial.
C Program To Find Transpose of a Matrix
// C Program To Find Transpose of a Matrix #include <stdio.h> int main(){ int i, j, row, column; int matrix[10][10], transpose[10][10]; //Asking for input printf("Enter rows and columns of the matrix: "); scanf("%d %d", &row, &column); printf("Enter elements of the matrix: \n"); for (i = 0; i < row; ++i) for (j = 0; j < column; ++j){ scanf("%d", &matrix[i][j]); } //Computing the transpose for (i = 0; i < row; ++i){ for (j = 0; j < column; ++j){ transpose[j][i] = matrix[i][j]; } } //Displaying the result printf("Transpose of the entered matrix is: \n"); for (i = 0; i < column; ++i){ for ( j = 0; j < row; ++j){ printf("%d \t", transpose[i][j]); } printf("\n"); } return 0; }
Output
Enter rows and columns of the matrix: 2 3
Enter elements of the matrix:
1 2 3
4 5 6
Transpose of the entered matrix is:
1 4
2 5
3 6
How Does This Program Work ?
int i, j, row, column; int matrix[10][10], transpose[10][10];
In this program, we have declared some int data type variables which will store the order and elements of the matrix.
//Asking for input printf("Enter rows and columns of the matrix: "); scanf("%d %d", &row, &column); printf("Enter elements of the matrix: \n"); for (i = 0; i < row; ++i) for (j = 0; j < column; ++j){ scanf("%d", &matrix[i][j]); }
Then, the user is asked to enter the order and elements of the matrix.
//Computing the transpose for (i = 0; i < row; ++i){ for (j = 0; j < column; ++j){ transpose[j][i] = matrix[i][j]; } }
We obtained the transpose by interchanging the rows and columns of the original matrix.
//Displaying the result printf("Transpose of the entered matrix is: \n"); for (i = 0; i < column; ++i){ for ( j = 0; j < row; ++j){ printf("%d \t", transpose[i][j]); } printf("\n"); }
Finally, the transpose matrix is displayed to the screen using printf() function.
Conclusion
I hope after going through this post, you understand how to find the transpose of a matrix using the C Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read: