You should write something similar to:
int product(float a[20][20],float b[20][20], float c[20][20])
{
}
and then , in the
main
function:
float a[r1][c1];
float b[r2][c2];
float c[r2][c2];
product(a,b,c);
[UPDATE]
N Shiva wrote:
float a[r1][c1];
float b[r2][c2];
These statements are
evil wrong (at compile time
r1
,
c1
, .. values are indefinite)!
A working (although still ugly) version of your program:
#include<stdio.h>
int product(float a[20][20], float b[20][20], float c[20][20]);
int i,j,k,r1,c1,r2,c2;
int main()
{
printf("Enter no.of rows of first matrix:\t");
scanf("%d",&r1);
printf("Enter no.of columns of first matrix:\t");
scanf("%d",&c1);
printf("Enter no.of rows of second matrix:\t");
scanf("%d",&r2);
printf("Enter no.of columns of second matrix:\t");
scanf("%d",&c2);
float a[20][20];
float b[20][20];
float c[20][20];
if ( product(a,b,c) ) return -1;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%f ",c[i][j]);
}
printf("\n");
}
return 0;
}
int product(float a[20][20], float b[20][20], float c[20][20])
{
if(c1==r2)
{
printf("Enter the elements of first matrix:\t");
for(i=0;i<r1;i++)
{ for(j=0;j<c1;j++)
{
scanf("%f",&a[i][j]);
}
}
printf("Enter the elements of second matrix:\t");
for(i=0;i<r2;i++)
{ for(j=0;j<c2;j++)
{
scanf("%f",&b[i][j]);
}
}
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("\n");
}
}
else
{
printf("Please Enter proper matrices");
return -1;
}
return 0;
}
[/UPDATE]
[UPDATE 2]
N Shiva wrote:
float a[r1][c1];
float b[r2][c2];
Actually (I didn't know that) some compilers (e.g. GCC) by default implement this C99 extension (
arrays of variable length[
^]). However, you should use them properly, try:
#include<stdio.h>
int product(int r1, int c1, float a[r1][c1], int r2, int c2, float b[r2][c2], float c[r1][c2]);
int main()
{
int i,j,r1,c1,r2,c2;
printf("Enter no.of rows of first matrix:\t");
scanf("%d",&r1);
printf("Enter no.of columns of first matrix:\t");
scanf("%d",&c1);
printf("Enter no.of rows of second matrix:\t");
scanf("%d",&r2);
printf("Enter no.of columns of second matrix:\t");
scanf("%d",&c2);
float a[r1][c1];
float b[r2][c2];
float c[r1][c2];
if ( product(r1, c1, a, r2, c2, b,c) ) return -1;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%f ",c[i][j]);
}
printf("\n");
}
return 0;
}
int product(int r1, int c1, float a[r1][c1], int r2, int c2, float b[r2][c2], float c[r1][c2])
{
int i, j, k;
if(c1==r2)
{
printf("Enter the elements of first matrix:\t");
for(i=0;i<r1;i++)
{ for(j=0;j<c1;j++)
{
scanf("%f",&a[i][j]);
}
}
printf("Enter the elements of second matrix:\t");
for(i=0;i<r2;i++)
{ for(j=0;j<c2;j++)
{
scanf("%f",&b[i][j]);
}
}
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("\n");
}
}
else
{
printf("Please Enter proper matrices");
return -1;
}
return 0;
}
[/UPDATE 2]