#include
#include
//function prototypes
void print_mat(int**,int,int);
void mul(int**,int**,int**,int,int,int,int);
void main()
{
//double pointer for two dimentional array
int **a,**b,**c,r1,c1,r2,c2,i,j,k,sum;
clrscr();
printf("Enter Matrix A information:\n");
printf("Row 1:");
scanf("%d",&r1);
printf("Column 1:");
scanf("%d",&c1);
printf("Enter Matrix B information:\n");
printf("Row 2:");
scanf("%d",&r2);
printf("Column 2:");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Multiplication can not performed.");
getch();
return;
}
//memory allocation
a=(int **)malloc(sizeof(int*)*r1);
b=(int **)malloc(sizeof(int*)*r2);
c=(int **)malloc(sizeof(int*)*r1);//r1xc2
for(i=0;i
{
a[i]=(int *)malloc(sizeof(int)*c1);
c[i]=(int *)malloc(sizeof(int)*c2);
}
for(i=0;i
{
b[i]=(int *)malloc(sizeof(int)*c2);
}
//http://kvGroup4all.blogspot.com
for(i=0;i
{
for(j=0;j
{
printf("Enter A[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i
{
for(j=0;j
{
printf("Enter B[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}
}
//function calling
mul(a,b,c,r1,c1,r2,c2);
printf("Matrix A:\n");
print_mat(a,r1,c1);
printf("\nMatrix B:\n");
print_mat(b,r2,c2);
//printing answer
printf("Matrix C:\n");
print_mat(c,r1,c2);
getch();
}
//http://kvGroup4all.blogspot.com
void print_mat(int **a,int r,int c)
{
int i,j;
for(i=0;i
{
for(j=0;j
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
//user defined function for multiplication
void mul(int **a,int **b,int **c,int r1,int c1,int r2,int c2)
{
int i,j,k,sum;
for(i=0;i
{
for(j=0;j
{
sum=0;
for(k=0;k
{
sum+=a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
}
//http://kvGroup4all.blogspot.com
No comments :
Post a Comment