Saturday, 9 March 2013

/*A program in c to multiply two matrices*/

/*A Program Developed By Raghav Raj For Multiplying Two
  Square Matrices*/
#include<stdio.h>
#include<conio.h>
#define ROW 6
#define COL 6
void display(int [][COL],int,int,int,int);
void main()
{
 int i,j,rowA,colA,rowB,colB;
 int A[ROW][COL],B[ROW][COL],sum=0;
 int k;
 printf("\nEnter row and column for Matrix A: ");
 scanf("%d%d",&rowA,&colA);
 printf("\nEnter elments for Matrix A%d X %d : ",rowA*colA);
 for(i=0;i<rowA;i++)
 {
  for(j=0;j<colA;j++)
  { //clrscr();
   printf("\nEnter element for A[%d][%d] : ",i,j);
   scanf("%d",&A[i][j]);
  }
 }
 clrscr();
 printf("Enter row and column for Matrix B : ");
 scanf("%d%d",&rowB,&colB);
 printf("\nEnter element for Matrix B %d X %d :",rowB*colB);
 for(i=0;i<rowB;i++)
 {
  for(j=0;j<colB;j++)
  { //clrscr();
    printf("\nEnter elemnet for B[%d][%d] : ",i,j);
    scanf("%d",&B[i][j]);
  }
 }clrscr();
 printf("Matrix A and B Are  :\n\t");
 display(A,rowA,colA,9,2);
 gotoxy(1,rowA);
 printf("A X B =");
 gotoxy(18,rowA);
 printf(" X ");
 gotoxy(22,2);
 display(B,rowB,colB,22,2);
 gotoxy(34,rowA);
 printf(" =");
 //Inputting done in both Matrices
 //Now calculate the multiplication of Matrices A and B
 gotoxy(1,rowA+3);
 if(colA==rowB)
 {
 printf("\nMultiplication of Matrices A and B are :\n");
 for(i=0;i<rowA;i++)
 {
   for(k=0;k<colA;k++)
   {
     for(j=0;j<rowB;j++)
     {
       sum+=A[i][j]*B[j][k];
     }
   printf("  %d\t",sum);
   sum=0;
   }
   printf("\n");
 }
 }
 else
 { clrscr();
   printf("\nMultipltion of Matrices can not be performed");
   printf("\n\nBecause,Matrix A and B Does not Exist\n\n\t\tThanks....");
 }
 getch();
 }
 void display(int matA[][ROW],int r,int c,int x,int y)
 {
   int i,j;
   for(i=0;i<r;i++)
   {
     for(j=0;j<c;j++)
     {
       printf("%d   ",matA[i][j]);
     }
     y=y+1;
     gotoxy(x,y);
   }
 } 

No comments:

Post a Comment