Write a program to perform transpose for a given sparse matrix. ( Row major order)
#include <stdio.h>
int main() {
int x,y;
int matrix[10][10];
int transpose[10][10];
int row, col;
printf("Enter the rows of the matrix: ");
scanf("%d", &row);
printf("Enter the columns of the matrix: ");
scanf("%d", &col);
// Entering the elements of the matrix
printf("\nEnter matrix elements:\n");
for (x = 0; x < row; ++x)
for (y= 0; y < col; ++y) {
printf("Enter element a%d%d: ", x + 1, y + 1);
scanf("%d", &matrix[x][y]);
}
// displaying the matrix before transpose a[][]
printf("\nMatrix before transpose: \n");
for (x = 0; x < row; ++x)
for (y = 0; y < col; ++y) {
printf("%d ", matrix[x][y]);
if (y == col - 1)
printf("\n");
}
// Calculating the transpose
for ( x = 0; x < row; ++x)
for ( y = 0; y < col; ++y) {
transpose[y][x] = matrix[x][y];
}
// Displaying the transpose
printf("\nTThe transpose matrix is: \n");
for ( x = 0; x< col; ++x)
for (y= 0; y < row; ++y) {
printf("%d ", transpose[x][y]);
if (y == row - 1)
printf("\n");
}
return 0;
}
Comments
Leave a comment