#include
#include
void main()
{
int a[10][10];
int b[10][10];
int add[10][10];
int mult[10][10];
int arows,acols;
int brows,bcols;
int sumroes,sumcols;
int multrows,multcols;
clrscr();
printf("Enter the number of rows and columns for A ");
scanf("%d %d",&arows,&acols);
printf("Enter the number of rows and columns for B ");
scanf("%d %d",&brows,&bcols);
printf("Enter elements for Matrix A \n");
for(int i = 0;i<=arows-1;i++)
for(int j=0;j<=acols-1;j++)
{
printf("Enter the element A[%d][%d] ",i,j);
scanf("%d",&a[i][j]);
}
printf("Enter elements for Matrix B\n");
for(i = 0;i<=brows-1;i++)
for(j=0;j<=bcols-1;j++)
{
printf("Enter the element B[%d][%d] ",i,j);
scanf("%d",&b[i][j]);
}
printf("Matrix A is \n");
for(i = 0;i<=arows-1;i++)
{
for(j=0;j<=acols-1;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("Matrix B is \n");
for(i = 0;i<=brows-1;i++)
{
for(j=0;j<=bcols-1;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
if(acols!=brows)
printf("A * B is not possible \n");
else
{
printf("A * B is \n");
for(i = 0;i<=arows-1;i++)
{
for(j = 0;j<=bcols-1;j++)
{
mult[i][j] = 0;
for(int k=0;k<=acols-1;k++)
mult[i][j] = mult[i][j] + a[i][k] * b[k][j];
printf("%d\t",mult[i][j]);
}
printf("\n");
}
}
if(arows!=bcols)
printf("B * A is not possible \n");
else
{
printf("B * A is \n");
for(i = 0;i<=brows-1;i++)
{
for(j = 0;j<=acols-1;j++)
{
mult[i][j] = 0;
for(int k=0;k<=arows-1;k++)
mult[i][j] = mult[i][j] + b[i][k] * a[k][j];
printf("%d ",mult[i][j]);
}
printf("\n");
}
}
getch();
}
This program will first check if A *B is possible or not, then only it will calculate the resulting matrix.
2006-11-05 20:52:05
·
answer #1
·
answered by manoj Ransing 3
·
0⤊
1⤋