Q2

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
922 B
2
Indexable
Never
#include <stdio.h>
int main(){
  int rows, columns;
  printf("Enter number of rows:");
  scanf("%d",&rows);
  printf("Enter number of columns:");
  scanf("%d",&columns);
  int matrix[rows][columns]; /* use this format for input */
  int transpose[columns][rows];
  for (int i=0;i<rows;i++){
    for (int j=0;j<columns;j++){
      printf("Enter matrix[%d][%d]:",i,j);
      scanf("%d",&matrix[i][j]);
    }
  }
  puts("");
  printf("2d Matrix:");
  for (int i=0;i<rows;i++){
    puts("");
    for(int j=0;j<columns;j++){
      printf("%d\t",matrix[i][j]);
    }
  }
  for (int i=0;i<rows;i++){
      puts("");
      for (int j=0;j<columns;j++){
          transpose[j][i]=matrix[i][j];
      }
  }
  puts("Transpose Matrix:");
  for (int column = 0; column < columns; column++)
    {
      for (int row = 0; row < rows; row++)
        {
          printf("%-4d", matrix[row][column]);
        }
      putchar('\n');
    }
  }