1.10.2012

Transpose of matrix


Q. Write a C program to accept value of matrix and find the transpose matrix.

Ans.

/*program of transpose of matrix*/
#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void trns_mat(int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values of %d X %d matrix :\n",row,col);
 input_mat(x,row,col);
 printf("\nYour entered matrix is : \n");
 show_mat(x,row,col);
 trns_mat(x,z,row,col);
 printf("\nTranspose of entered matrix is :\n");
 show_mat(z,row,col);
 return 0;
}

void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void trns_mat(int matA[MAX][MAX],int matT[MAX][MAX],int r,int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       matT[j][i] matA[i][j];
  }
}


void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("\n");
  }
}


The output of above program would be:

Output of Transpose of matrix C program
Figure: Screen shot for transpose of matrix C program


You might also like:
  1. Difference of two matrix C program
  2. Product of matrix C program
  3. Sum of diagonal elements C program
  4. Sum of two matrix C program

5 comments:

  1. good logic.......

    ReplyDelete
  2. But where have you declared 'matB'? You might have missed out on a line. Otherwise good logic.

    ReplyDelete
    Replies
    1. @Diwakar Ray,
      Yeah, that is mistake!
      Now i re-write code of above program with screen-shot of output.

      Delete
    2. hey i cant understand this
      it is so tough

      Delete
    3. this program is wrong.. it shows garbage value when 2*3 matrix is taken

      Delete