Find Transpose of a Matrix in C language using Array
Find transpose of a matrix in C language using the array. In this article, you will learn how to find transpose of a matrix in c language using the array.
The transpose of a matrix in linear algebra is an operator which flips a matrix over its diagonal.
This switches the rows and columns indices of matrix A by producing another matrix.
Source Code
// Find Transpose of a Matrix in C language using Array
#include <stdio.h>
int main() {
int r, c, i, j;
// r - denotes the matrix's rows
// c - dentes the matrix's columns
printf("-----Enter the number of rows & columns of the matrix-----\n");
scanf("%d %d", &r, &c);
int a[r][c], t[c][r];
// a - denotes the input matrix
// t - denotes the transpose matrix
printf("\n-----Enter the matrix's elements-----\n");
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
printf("Enter element at position[%d%d]: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\n");
}
// It will display the input matrix
printf("\n-----The entered matrix-----\n");
for (i = 0; i < r; ++i) {
printf("\t");
for (j = 0; j < c; ++j) {
printf("%d\t", a[i][j]);
if (j == c - 1)
printf("\n");
}
printf("\n");
}
// It will transposes matrix
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
t[j][i] = a[i][j];
}
}
// It will display the transposed matrix
printf("\n-----The transpose of the matrix-----\n");
for (i = 0; i < c; ++i) {
printf("\t");
for (j = 0; j < r; ++j) {
printf("%d\t", t[i][j]);
if (j == r - 1)
printf("\n");
}
printf("\n");
}
return 0;
}
Output
-----Enter the number of rows & columns of the matrix-----
3
4
-----Enter the matrix's elements-----
Enter element at position[11]: 12
Enter element at position[12]: 34
Enter element at position[13]: 56
Enter element at position[14]: 23
Enter element at position[21]: 67
Enter element at position[22]: 32
Enter element at position[23]: 89
Enter element at position[24]: 23
Enter element at position[31]: 12
Enter element at position[32]: 23
Enter element at position[33]: 99
Enter element at position[34]: 23
-----The entered matrix-----
12 34 56 23
67 32 89 23
12 23 99 23
-----The transpose of the matrix-----
12 67 12
34 32 23
56 89 99
23 23 23