Displaying Source Code(s)
|
|
Program to Multiply Two Matrices.
--------------------------------------------------------------------------------
#define MAXROWS 30
#define MAXCOLS 30
void readinput(int a[][MAXCOLS],int m,int n);
void computeproduct(int a[][MAXCOLS],int b[][MAXCOLS],int
c[][MAXCOLS],int m,int n,int p);
void writeoutput(int c[][MAXCOLS],int m,int p);
int z=0;
void main()
{
/* char c;*/
int nrows,ncols,mrows,mcols;
int a[MAXROWS][MAXCOLS],b[MAXROWS][MAXCOLS],c[MAXROWS][MAXCOLS];
clrscr();
printf("
How many rows in the first matrix? ");
scanf("%d",&nrows);
printf("
How many cols in the first matrix? ");
scanf("%d",&ncols);
printf("
How many rows in the second matrix? ");
scanf("%d",&mrows);
printf("
How many cols in the second matrix? ");
scanf("%d",&mcols);
if (ncols != mrows)
{
printf("The product of these matrices is not defined.");
getch();
exit(0);
}
printf("
First table:<BR>);
readinput(a,nrows,ncols);
printf("
Second table:<BR>);
readinput(b,mrows,mcols);
computeproduct(a,b,c,nrows,ncols,mcols);
printf("
Product of the matrices is:
<BR>);
writeoutput(c,nrows,mcols);
getch();
}
void readinput(int a[][MAXCOLS],int m,int n)
{
int row,col;
for (row=0;row<m;row++)
{
printf("
Enter data for row no. %4d<BR>,row+1);
for (col=0;col<n;col++)
scanf("%d",&a[row][col]);
}
z=z+1;
printf(" Table %d<BR>,z);
for (row=0;row<m;row++)
{
for (col=0;col<n;col++)
printf("%d%c",a[row][col],' ');
printf("<BR>);
}
return;
}
void computeproduct(int a[][MAXCOLS],int b[][MAXCOLS],int
c[][MAXCOLS],int m,int n,int p)
{
int i,j,k,sum=0;
for (i=0;i<m;i++)
{
for (j=0;j<p;j++)
{
for (k=0;k<n;k++)
sum=sum+(a[i][k]*b[k][j]);
c[i][j]=sum;
sum=0;
}
}
return;
}
void writeoutput(int c[][MAXCOLS],int m,int p)
{
int row,col;
for (row=0;row<m;row++)
{
for (col=0;col<p;col++)
printf("%6d",c[row][col]);
printf("<BR>);
}
return;
}
|
|
|