Tuesday, March 22, 2011

Program for Transpose Of A Matrix with Example -2


Alternative Version of the Program

This is another version of the transpose program. Here a separate matrix is used to hold the result of transposition.

#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 b[][COL],int,int);
   int a[3][3],b[3][3],i,j;
   clrscr();
   read(a,ROW,COL);
   printf("\nThe matrix is \n");
   dis(a,ROW,COL);
   trans(a,b,ROW,COL);
   printf("The tranpose of the matrix is\n");
   dis(b,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 tr_mat[][3], int k ,int l)
{
   int i,j;
   for(i=0;i<k;i++)
   for(j=0;j<l;j++)
   {
          tr_mat[i][j]=mat[j][i];
   }
}

Example

Input
Enter the array 1
2
3
4
5
6
7
8
9
Output
The matrix is
1 2 3
4 5 6
7 8 9
The transpose of the matrix is
1 4 7
2 5 8
3 6 9

No comments:

Post a Comment