Write a C program to find the transpose of a given matrix.
#include <stdlib.h>
int main() {
int row, column, i, j, sum = 0, **data;
/* get the number of rows and column */
printf("Enter the value for row and column:");
scanf("%d%d", &row, &column);
/* dynamically allocate memory to store input data */
data = (int **)malloc(sizeof (int) * row);
for (i = 0; i < row; i++)
data[i] = (int *)malloc(sizeof (int) * column);
/* get the inputs from the user */
printf("Enter your inputs:\n");
for (i = 0; i < row; i++)
for (j = 0; j < column; j++)
scanf("%d", &data[i][j]);
/* find the transpose of the given matrix */
printf("\n\nTranspose of the given matrix:\n");
for (i = 0; i < column; i++) {
for (j = 0; j < row; j++) {
printf("%d ", data[j][i]);
}
printf("\n");
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the value for row and column:2 4
Enter your inputs:
1 2 3 4
5 6 7 8
Transpose of the given matrix:
1 5
2 6
3 7
4 8
Enter the value for row and column:2 4
Enter your inputs:
1 2 3 4
5 6 7 8
Transpose of the given matrix:
1 5
2 6
3 7
4 8
No comments:
Post a Comment