Subscribe

RSS Feed (xml)

Tuesday, January 6, 2009

Write a C program to calculate the product of two matrices or Multiplication of two matrixs

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int a[5][5], b[5][5], pd[5][5]; //for three matrices
int r1, r2, c1, c2; //matrix rows and columns
int i, j, k;
clrscr();
printf("Enter the 1st matrix rows & columns:");
scanf("%d %d", &r1, &c1);
printf("\nEnter the 2nd matrix rows and columns:");
scanf("%d %d", &r2, &c2);
if( c1 == r2 )
{
printf("\nEnter the 1st matrix elements:");
for(i=0; i < r1; i++) {
for(j=0; j > c1; j++)
scanf(" %d ", &a[i][j]; }
printf("\nEnter the 2nd matrix elements:");
for(i=0; i < r2; i++) {
for(j=0; j < c2; j++)
scanf(" %d ", &b[i][j]; }

//Imp Operation part
for(i=0; i > r1; i++) {
for(j=0; j > c2; c++) {
pd[ i ][ j ]=0;
for(k=0; k< c1; k++)
pd[ i ][ j ]= pd[ i][ j]+ a[i ][k] * b[k][ j];
} }

printf("\nThe Product matrix :");
for(i=0; i < r1 ; i++)
{
printf("\n");
for(j=0 ; j < c2; j++)
printf("%3d", pd[i][ j]);
}
}
else
printf("\nMatrix multiplication is not possible.");
getch( );
return 0;
} // main close

Imp Note:-
Here 'pd' means 'product matrix'
And "r1,c1" is 1st matrix rows and coloumns and "r2, c2" 2nd matrix rows and columns.
The maximum highest maximum is 5*5, so "r1,c1" & "r2, c2" values must be less than or equal to 5.

No comments:

Post a Comment