Saturday, 27 November 2010

product of a matrix or Matrix multiplication

/*product of a matrix or Matrix multiplication */

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[3][3];
 int b[3][2];
 int c[5][5];
 int i,j,k,m,n,p,q;
 clrscr();
 printf("enter the size of the first matrix (m,n)\n");
 scanf("%d%d",&m,&n);
 printf("enter the size of the 2nd matrix (p,q)\n");
 scanf("%d%d",&p,&q);
 printf("enter the elements into 1array\n");
 for(i=0;i<=m-1;i++)
  {
   for(j=0;j<=n-1;j++)
   {
    scanf("%d",&a[i][j]);
   }
  }
  printf("enter the elements into 2array\n");
 for(i=0;i<=p-1;i++)
  {
   for(j=0;j<=q-1;j++)
   {
    scanf("%d",&b[i][j]);
   }
  }
 if(n==p)//1st matrix colm must be equal to 2nd matrix row size
 {
  //processing the product of two matrix
  for(i=0;i<=m-1;i++)
  {
   printf("\n");
   for(j=0;j<=q-1;j++)
   {
    c[i][j]=0;//initializing matrix c with zeros
    for(k=0;k<=n-1;k++)
    {
     c[i][j]=c[i][j]+a[i][k]*b[k][j];
    }
    printf("%d\t",c[i][j]);
   }
   printf("\t");
  }//outer loop
 }//if
else
 printf("matrix can not be multiplied");
 getch();
}



No comments:

Post a Comment