About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Monday, September 2, 2013

C Program to Multiply Two 3 X 3 Matrices

#include<stdio.h>
#include<conio.h>

void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k;
int sum=0;
clrscr();


printf("\nEnter First Matrix : n");

for(i=0;i<3;i++)
   {
   for(j=0;j<3;j++)
   scanf("%d",&a[i][j]);
   }

printf("\nEnter Second Matrix:n");
for(i=0;i<3;i++)
   {
   for(j=0;j<3;j++)
   scanf("%d",&b[i][j]);
   }

printf("The First Matrix Is:n");

for(i=0;i<3;i++)//print the first matrix
   {
   for(j=0;j<3;j++)
   printf(" %d ",a[i][j]);
   printf("\n");
   }

printf("The Second Matrix Is:n");

for(i=0;i<3;i++) // print the second matrix
   {
   for(j=0;j<3;j++)
   printf(" %d ",b[i][j]);
   printf("\n");
   }


for(i=0;i<=2;i++)
   for(j=0;j<=2;j++)
   {
   sum = 0;
    for(k=0;k<=2;k++)
     sum = sum + a[i][k] * b[k][j];
   c[i][j]=sum;
   }

printf("\nMultiplication Of Two Matrices :nn");

for(i=0;i<3;i++)
{
   for(j=0;j<3;j++)
   printf(" %d ",c[i][j]);
printf("\n");
}

getch();
}

Steps :
Dimension of Matrix 1 : 1 X 3
Dimension of Matrix 2 : 3 X 2
Multiplication is Possible iff -
No. of Columns of Matrix 1 = No of Columns of Matrix 2
Resultant Matrix Will of Dimension-
c[No. of Rows of Mat1][No. of Columns of Mat2]
Steps 1 :
Evaluate : 1 X 2  = 2
Evaluate : 4 X 5  = 20
Evaluate : 6 X 7  = 22
-------------------------
Add      :          64  ///c[0][0]
Step 2 :
Evaluate : 1 X 3  = 3
Evaluate : 4 X 8  = 40
Evaluate : 6 X 9  = 54
-------------------------
Add      :          89   ///c[0][1]
Programmable Implementation :
for(i=0;i<=2;i++)
   for(j=0;j<=2;j++)
   {
   sum = 0;
    for(k=0;k<=2;k++)
     sum = sum + a[i][k] * b[k][j];
   c[i][j]=sum;
   }

No comments: