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:
");
readinput(a,nrows,ncols);
printf("

Second table:
");
readinput(b,mrows,mcols);

computeproduct(a,b,c,nrows,ncols,mcols);

printf("

Product of the matrices is:

");
writeoutput(c,nrows,mcols);

getch();
}


void readinput(int a[][MAXCOLS],int m,int n)
{
int row,col;
for (row=0;row
{
printf("
Enter data for row no. %4d
",row+1);
for (col=0;col
scanf("%d",&a[row][col]);
}
z=z+1;
printf(" Table %d
",z);
for (row=0;row
{
for (col=0;col
printf("%d%c",a[row][col],' ');
printf("
");
}
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
{
for (j=0;j
{
for (k=0;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
{
for (col=0;col
printf("%6d",c[row][col]);
printf("
");
}
return;
}

Related Links :

1 comment:


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!