Answer to Question #232890 in C for Asjad

Question #232890

Write a program to perform transpose for a given sparse matrix. ( Row major order) 


1
Expert's answer
2021-09-03T15:07:52-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS