Friday, July 15, 2011

Source Code for Performing Matrix Multipliaction in C


The program below demostrates how to multiply any two matrices in c.Before multiplication the order of the matrix is checked to ensure multiplication is possible.

 #include<stdio.h>  
 #include<conio.h>  
 void main()  
 {  
  int A[50][50],B[50][50],C[50][50];  
  int i,j,p,r1,r2,c1,c2;  
  clrscr();  
  printf("\nMATRIX MULTIPLICATION");  
  printf("\nEnter The order Of First Matrix:");  
  scanf("%d%d",&r1,&c1);  
  printf("\nEnter The Elements Of First Matrix:");  
  for(i=0;i<r1;i++)  
   for(j=0;j<c1;j++)  
     scanf("%d",&A[i][j]);  
  printf("\n\nEnter The order Of Second Matrix:");  
  scanf("%d%d",&r2,&c2);  
  printf("\nEnter The Elements Of Second Matrix:");  
  for(i=0;i<r2;i++)  
   for(j=0;j<c2;j++)  
     scanf("%d",&B[i][j]);  
  if(c1!=r2)  
  {  
   printf("\n\nThe Matrix Cant Be Multipied!!!");  
   getch();  
   exit(0);  
  }  
  for(i=0;i<r1;i++)  
   for(j=0;j<c2;j++)  
   {  
    C[i][j]=0;  
    for(p=0;p<c1;p++)  
     C[i][j]+=A[i][p]*B[p][j];  
   }  
   printf("\n\nThe Product Of Matrices Is:");  
  for(i=0;i<r1;i++)  
  { printf("\n");  
    for(j=0;j<c2;j++)  
     printf("%d ",C[i][j]);  
  }  
  getch();  
 }   
}

1 comments:

Post a Comment

Subscribe

The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.