Tuesday, March 22, 2011

Program for Transpose Of A Matrix with Example


Program

#include<stdio.h>
#include<conio.h>
#define ROW 3
#define COL 3

void main()
{
   void read(int a[][COL],int,int);
   void dis(int a[][COL],int,int);
   void trans(int a[][COL],int,int);
   int a[3][3];
   clrscr();
   read(a,ROW,COL);
   printf("\nThe matrix is \n");
   dis(a,ROW,COL);
   trans(a,ROW,COL);
   printf("The tranpose of the matrix is\n");
   dis(a,ROW,COL);
   getch();
}
void read(int c[3][3] ,int i ,int k)
{
    int j,l;
   printf("Enter the array \n");
   for(j=0;j<i;j++)
      for(l=0;l<k;l++)
             scanf("%d",&c[j][l]);
   fflush(stdin);
}
void dis(int d[3][3 ],int i,int k)
{
   int j,l;
   for(j=0;j<i;j++)
   {
      for(l=0;l<k;l++)
             printf("%d ",d[j][l]);
      printf("\n");
   }
}
void trans(int mat[][3],int k ,int l)
{
   int i,j,temp;
   for(i=0;i<k;i++)
       for(j=i+1;j<l;j++)
      {
         temp=mat[i][j];
         mat[i][j]=mat[j][i];
         mat[j][i]=temp;
      }
}
Example
Input
Enter the array
12
13
14
15
16
17
18
19
11
Output
The matrix is
12 13 14
15 16 17
18 19 11
The transpose of the matrix is
12 15 18
13 16 19
14 17 11

No comments:

Post a Comment