5.B) Write a C program to transpose a m x n matrix.
Answer:
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int m = 0; m < r; ++m)
for (int n = 0; n < c; ++n) {
printf("Enter element a%d%d: ", m + 1, n + 1);
scanf("%d", &a[m][n]);
}
printf("\nEntered matrix: \n");
for (int m = 0; m < r; ++m)
for (int n = 0; n < c; ++n) {
printf("%d ", a[m][n]);
if (n == c - 1)
printf("\n");
}
for (int m = 0; m < r; ++m)
for (int n = 0; n < c; ++n) {
transpose[n][m] = a[m][n];
}
printf("\nTranspose of the matrix:\n");
for (int m = 0; m < c; ++m)
for (int n = 0; n < r; ++n) {
printf("%d ", transpose[m][n]);
if (n == r - 1)
printf("\n");
}
return 0;
}