Thursday, 21 June 2012

write a c program for calculate the multipication of m*n matrix


/* Assignment number=5 */
/*program for calculate the multipication of m*n matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int r1,c1,r2,c2,i,j,k;
clrscr();
printf("\nenter the rows & coloumn of a matrix : ");
scanf("%d%d",&r1,&c1);
printf("\nenter the rows & coloumn of matrix b :");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("\nthe matrix can be multiplied");
printf("\nresultant matrix id %d,%d\n",r1,c2);
printf("enter the element of matrix a :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
printf("\n");
}
printf("enter the element of matrix b :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("the resultant matrix of c is :\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
}
}
else
printf("invalid input");
getch();
}
/*===========================output=================*/
/*
enter the rows & coloumn of a matrix : 3 2

enter the rows & coloumn of matrix b :2 4

the matrix can be multiplied
resultant matrix id 3,4
enter the element of matrix a :
2 5

1 4

2 3

enter the element of matrix b :
1 4 5 2

1 3 2 1


the resultant matrix of c is :

7       23      20      9
5       16      13      6
5       17      16      7      */

No comments: