#include<stdio.h>
float product(float[20][20],float[20][20]);
int i,j,k,r1,c1,r2,c2;
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[r1][c1];
float b[r2][c2];
product(a,b);
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%f",c[i][j]);
}
}
}
float product(float a[20][20],float b[20][20])
{ float c[r1][c2];
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");
}
This program takes two matrices as inputs and gives their product. When I have written this program without function I got the solutions. I want to use this operation in other program, that is why I need to write this as a function. But Iam getting this error.
Thnx in advance.
matr.c: In function ‘main’:
matr.c:24: error: ‘c’ undeclared (first use in this function)
matr.c:24: error: (Each undeclared identifier is reported only once
matr.c:24: error: for each function it appears in.)
Important Info from OP copied from comment below:
My sole intention to write this function is, I use it in b/w my program related to Graph theory. where I may use product(product(x,y),z) to get x*y*z.