//Matrix Multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int r1,c1,r2,c2,i,j,k;
int a[8][8],b[8][8],c[8][8];
int sum=0;
clrscr();
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
{
printf("Multiplication is not possible");
}
else
{
printf("\nEnter the elements for 1st matrix\n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("The First Matrix is: \n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
printf(" %d ", a[i][j]);
}
printf("\n");
}
printf("\nEnter elements of 2nd matrix\n");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("The Second Matrix is : \n");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
printf(" %d ", b[i][j]);
}
printf("\n");
}
//Matrix Multiplication
for (i = 0; i <= r1; i++)
{
for (j = 0; j <= c2; j++)
{
sum = 0;
for (k = 0; k <= c1; k++)
{
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
//Display of matrix after multiplication
printf("\nMultiplication Of Two Matrices : \n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
printf(" %d ", c[i][j]);
}
printf("\n");
}
}
getch();
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.