Find the transpose of a matrix with C.
#include <stdio.h>
int main()
{
int A[3][3]; // Original matrix
int B[3][3]; // Transpose matrix
int row, col;
/* Input elements in matrix A from user */
printf("Enter elements in matrix of size %dx%d: \n", 3, 3);
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
scanf("%d", &A[row][col]);
}
}
/*
* Find transpose of matrix A
*/
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
/* Store each row of matrix A to each column of matrix B */
B[col][row] = A[row][col];
}
}
/* Print the original matrix A */
printf("\nOriginal matrix: \n");
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
printf("%d ", A[row][col]);
}
printf("\n");
}
/* Print the transpose of matrix A */
printf("Transpose of matrix A: \n");
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
printf("%d ", B[row][col]);
}
printf("\n");
}
return 0;
}
Output
Enter elements in matrix of size 3x3 :
1 2 3
4 5 6
7 8 9
Original matrix :
1 2 3
4 5 6
7 8 9
Transpose of matrix A :
1 4 7
2 5 8
3 6 9
0 Comments