Matrix PRGM
Matrix PRGM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][3], b[3][4], c[2][4];
int i,j,k;
printf("Enter elements of the first matrix of order 2 x 3 \n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of the second matrix of order 3 x 4 \n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nFirst Matrix is \n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nSecond Matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\nMatrix multiplication is \n");
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
A prerequisite for multiplying two matrices is that the number of columns in the
first matrix must be equal to the number of rows in the second matrix.
Steps:-
4. The element first row, first column in the resultant matrix is computed
using the following formula:
SUM(first element of the first row of the first matrix × first element of the first
column of the second matrix), (second element of the first row... × second element
of the first column...), (and so on...)
For example, let's assume the elements of the two matrices .The elements in the
first row and the first column of the resultant matrix will be computed as follows:
5. Hence, the element in first row, first column in the resultant matrix will be
as follows:
(3×6)+(9×3)+(7×5)
=18 + 27 + 35
=80