Got a program to multiply 2 matrices. Below is my source code:
Code:
#include "stdio.h"
#include "conio.h"
void main()
{
static int a[10] [10];
static int b[10] [10];
static int c[10] [10];
int i,j,k,l,m,n,p,q;
clrscr();
printf ("Put the dimension for array A ,i.e. row x column \n");
scanf ("%d %d",&m,&n);
printf ("Put the dimension for array B ,i.e. row x column \n");
scanf ("%d %d",&p,&q);
if (n!=p)
{
printf("Multiplication of matrices is not possible \n");
printf ("Dimension of matrices is not valid");
}
else
{
printf ("Input the element in the array A \n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
scanf ("%d",&a[i] [j]);
}
}
printf("\nThe array A is as follows \n");
for(i=0; i<m; i++)
{
for(j=0;j<n;j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
printf ("\nInput the elements in the array B \n");
for (k=0; k<p; k++)
{
for (l=0; l<q; l++)
{
scanf ("%d",&b[k] [l]);
}
}
printf("\nThe array b is as follows: \n");
for(k=0;k<p;k++)
{
for(l=0;l<q;l++)
{
printf("%d ", b[k][l]);
}
printf("\n");
}
for (i=0; i<m; i++)
{
for (l=0; l<q; l++)
{
for (j=0; j<n; j++)
{
c[i] [l] = c[i] [l] + a[i] [j] * b[j] [l];
}
}
}
printf("\n");
printf("The result is as follows: \n");
for (i=0; i<m; i++)
{
l=0;
while (l<q)
{
printf (" %d ",c[i] [l]);
l++;
}
printf ("\n");
}
}
getch();
}
Do u think I can make it simpler?