Tuesday, July 2, 2013

C PROGRAM FOR 2D MATRIX MULTIPLICATION

/*PROGRAM TO MULTIPLY TWO MATRICES*/

#include<stdio.h>
#include<conio.h>
void main() {
int i,j,k,m,n,o,p,sum=0;
int a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter order of 1st 2D matrix:");
scanf("%d %d",&m,&n);
printf("Enter order of 2nd 2D matrix");
scanf("%d %d",&o,&p);
if(n==o){
printf("Enter elements of 1st 2d matrix:");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++){
                  printf("\na[%d][%d]=", i,j);
                  scanf("%d", &a[i][j]);
                  }
printf("Enter elements of 2nd 2D matrix:");
for(i=1;i<=o;i++)
for(j=1;j<=p;j++){
                  printf("\nb[%d][%d]=", i,j);
                  scanf("%d", &b[i][j]);
                  }
printf("\nThe product of two matrices has these elements:");
for(i=1;i<=m;i++)
for(j=1;j<=p;j++){
                  for(k=1;k<=n;k++)
                  sum=sum+a[i][k]*b[k][j];
                  c[i][j]=sum;
                  sum=0;
                  printf("\nc[%d][%d]=%d", i,j,c[i][j]);
                  }                
}
else
printf("The matrices cannot be multiplied");
getch();
}

/* OUTPUT:-
Enter order of 1st 2D matrix:1
2
Enter order of 2nd 2D matrix2
3
Enter elements of 1st 2d matrix:
a[1][1]=1

a[1][2]=2
Enter elements of 2nd 2D matrix:
b[1][1]=3

b[1][2]=4

b[1][3]=5

b[2][1]=6

b[2][2]=7

b[2][3]=8

The product of two matrices has these elements:
c[1][1]=15
c[1][2]=18
c[1][3]=21  */

No comments:

Post a Comment