Write a Program for Transpose of Matrix in C.
Example,
Input 1 2 3 4 5 6 7 8 9 Output 1 4 7 2 5 8 3 6 8 Input 1 2 3 4 5 7 8 9 Output 1 5 2 7 3 8 4 9
The Transpose of a Matrix is a new Matrix in which the rows are the columns of the original Matrix.
The Programs first ask user to enter the Number of rows and Number of columns of the Matrix.
After that, ask the user to enter the elements of the array ( row – wise ).
The new Matrix is stored is a separate Matrix.
Program for Transpose of a Matrix in C
#include<stdio.h>
#include<stdlib.h>
int main() {
int i, j;
int r, c;
int matrix[50][50], matrixT[50][50];
printf("Enter Number of Rows in Matrix: ");
scanf("%d", &r);
printf("Enter Number of columns in Matrix: ");
scanf("%d", &c);
printf("Enter Matrix: \n");
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
scanf("%d", &matrix[i][j]);
}
}
// Transposing Matrix
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
matrixT[j][i] = matrix[i][j];
}
}
printf("Transpose of Matrix: \n");
for (i = 0; i < c; ++i) {
for (j = 0; j < r; ++j) {
printf("%d ",matrixT[i][j]);
}
printf("\n");
}
}
Output
What to Study Next?