Friday 14 October 2011

Source code to transpose a matrix


//program to transpose a matrix//


#include<stdio.h>
#include<conio.h>
#define max 5
void main()
{
  int i,j,m,n,a[max][max],b[max][max];
  clrscr();
  printf("\t\t\t**** TRANSPOSE OF A MATRIX ****");
  printf("\nEnter the no. of rows & coloumns:\n");
  scanf("%d%d",&m,&n);
  printf("Enter the elements of matrix:\n");
  for(i=0;i<m;i++)
  {
     for(j=0;j<n;j++)
     {
      scanf("%d",&a[i][j]);
      b[j][i]=a[i][j];
     }
  }
  printf("\nInputed Matrixes is:\n\n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
      printf("%-4d\t",a[i][j]);      //4 digit right alignment
    }
    printf("\n");
  }
  printf("\nTransposed matrix is:\n\n");
  for(i=0;i<n;i++)
  {
  for(j=0;j<m;j++)
    {
      printf("%-4d\t",b[i][j]);      //4 digit right alignment
    }
    printf("\n");
  }
  getch();
}

No comments:

Post a Comment