In this program , we have to input the elements in 2 2D arrays which are a[ ][ ] & b[ ][ ] & multiply them. So , the no. of rows & columns are stored in m & n respectively . The matrix can only be added if no. of rows & cloumns of both matrices are same i.e. if it is a quare matrix . Then the values of 2 matrices a[ ][ ] & b[ ][ ] are inserted. how matrices are multiplied : -
c[00] = a[00]*b[00] + a[01]*b[00]
c[01] = a[00]*b[01] + a[01]*b[11]
c[10] = a[10]*b[00] + a[11]*b[10]
c[11] = a[10]*b[01] + a[11]*b[11]
Using this procedure & matrices are multiplied & result is stored in matrix c[ ][ ]..
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],b[10][10],c[10][10],i=0,j=0,m,n,p,q,k;
clrscr();
printf("\n program to multiply 2 matrices");
printf("\n (column of 1st & rows of 2nd should be same to multiply)");
printf("\n enter no. of rows (1st matrix) : ");
scanf("%d",&m);
printf(" enter no. of column (1st matrix) : ");
scanf("%d",&n);
printf("enter no. of rows (2nd matrix) : ");
scanf("%d",&p);
printf(" enter no. of column (2nd matrix) : ");
scanf("%d",&q);
if(n==p)
{ printf("\n now enter 1st matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n now enter 2nd matrix : \n");
for(i=0;i<p;i++)
{ for(j=0;j<q;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\n matrix after multiplication = \n");
for(i=0;i<m;i++)
{ for(j=0;j<q;j++)
{ c[i][j] = 0;
for(k=0;k<m;k++)
{ c[i][j] = c[i][j] + (a[i][k] + b[k][j]);
printf("\t %d",c[i][j]);
}
}
printf("\n");
}
}
else printf("Can't multiply bcz column of 1st & rows of 2nd matrix does not match");
getch();
}
c[00] = a[00]*b[00] + a[01]*b[00]
c[01] = a[00]*b[01] + a[01]*b[11]
c[10] = a[10]*b[00] + a[11]*b[10]
c[11] = a[10]*b[01] + a[11]*b[11]
Using this procedure & matrices are multiplied & result is stored in matrix c[ ][ ]..
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],b[10][10],c[10][10],i=0,j=0,m,n,p,q,k;
clrscr();
printf("\n program to multiply 2 matrices");
printf("\n (column of 1st & rows of 2nd should be same to multiply)");
printf("\n enter no. of rows (1st matrix) : ");
scanf("%d",&m);
printf(" enter no. of column (1st matrix) : ");
scanf("%d",&n);
printf("enter no. of rows (2nd matrix) : ");
scanf("%d",&p);
printf(" enter no. of column (2nd matrix) : ");
scanf("%d",&q);
if(n==p)
{ printf("\n now enter 1st matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n now enter 2nd matrix : \n");
for(i=0;i<p;i++)
{ for(j=0;j<q;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\n matrix after multiplication = \n");
for(i=0;i<m;i++)
{ for(j=0;j<q;j++)
{ c[i][j] = 0;
for(k=0;k<m;k++)
{ c[i][j] = c[i][j] + (a[i][k] + b[k][j]);
printf("\t %d",c[i][j]);
}
}
printf("\n");
}
}
else printf("Can't multiply bcz column of 1st & rows of 2nd matrix does not match");
getch();
}

No comments:
Post a Comment
Guys if you think something is wrong or should be edit than please do comment.