Write a C program to find transpose of a matrix.
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main() {
int m1[MAXROW][MAXCOL], t1[MAXROW][MAXCOL];
int i, j, row, col;
/* get the number of rows for the input matrix */
printf("Enter the number of rows:");
scanf("%d", &row);
/* get the number of columns for the input matrix */
printf("Enter the number of columns:");
scanf("%d", &col);
/* get the entries for the input matrix */
printf("Enter the entries for your input matrix:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
scanf("%d", &m1[i][j]);
}
}
/* finding transpose for the given matrix */
for (i = 0; i < col; i++) {
for (j = 0; j < row; j++) {
t1[i][j] = m1[j][i];
}
}
#define MAXROW 10
#define MAXCOL 10
int main() {
int m1[MAXROW][MAXCOL], t1[MAXROW][MAXCOL];
int i, j, row, col;
/* get the number of rows for the input matrix */
printf("Enter the number of rows:");
scanf("%d", &row);
/* get the number of columns for the input matrix */
printf("Enter the number of columns:");
scanf("%d", &col);
/* get the entries for the input matrix */
printf("Enter the entries for your input matrix:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
scanf("%d", &m1[i][j]);
}
}
/* finding transpose for the given matrix */
for (i = 0; i < col; i++) {
for (j = 0; j < row; j++) {
t1[i][j] = m1[j][i];
}
}
/* prints the transpose of the given matrix */
printf("Transpose of the given matrix:\n");
for (i = 0; i < col; i++) {
for (j = 0; j < row; j++) {
printf("%d ", t1[i][j]);
}
printf("\n");
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the number of rows:3
Enter the number of columns:2
Enter the entries for your input matrix:
10 20
30 40
50 60
Transpose of the given matrix:
10 30 50
20 40 60
Enter the number of rows:3
Enter the number of columns:2
Enter the entries for your input matrix:
10 20
30 40
50 60
Transpose of the given matrix:
10 30 50
20 40 60
No comments:
Post a Comment