Two matrices that have the same number of rows and columns can be multiplied to produce a third matrix.The product of A and B is a third matrix C of size nxn where each elements of C is given by the following equation.
Cij = aikbkj
Write a program that will read the values of elements of A and B and produce the product matrix C.
#include <stdio.h>
main()
{
int a[10][10],b[10][10],c[100][100]={0},i,j,k,n;
clrscr();
printf("Enter no. of Rows and column:");
scanf("%d",&n);
printf("Enter the elements of first %dx%d matrix\n",n,n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("Enter the element of second %dx%d matrix\n",n,n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&b[i][j]);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("%4d",c[i][j]);
}
putchar('\n');
}
getch();
}
Cij = aikbkj
Write a program that will read the values of elements of A and B and produce the product matrix C.
#include <stdio.h>
main()
{
int a[10][10],b[10][10],c[100][100]={0},i,j,k,n;
clrscr();
printf("Enter no. of Rows and column:");
scanf("%d",&n);
printf("Enter the elements of first %dx%d matrix\n",n,n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("Enter the element of second %dx%d matrix\n",n,n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&b[i][j]);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("%4d",c[i][j]);
}
putchar('\n');
}
getch();
}
No comments:
Post a Comment