`printf("%f",c[i][j]);`

You have not declared "c" in this statment anywhere. To use c in main you have to declare it just like you did for a and b.

product(a,b);Does nothing because you are throwing the result away.

/*Multiplication of two matrices*/ #include<stdio.h> float product(float[20][20],float[20][20]); int i,j,k,r1,c1,r2,c2; main() { /*c1=r2*/ 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) { /*scanning elements*/ 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]); } } /*scanning cmpltd*/ /*multiplication*/ 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.)
```

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.

Comments

`printf("%f",c[i][j]);`

You have not declared "c" in this statment anywhere. To use c in main you have to declare it just like you did for a and b.

product(a,b);Does nothing because you are throwing the result away.

Comments

How can I catch the result in that function. I mean c[i][j].

I just want to store that in array c.

I just want to store that in array c.

Try declaring c in your main function and pass it to your product function. Might be easier to pass it as a pointer.

right..+5

Sorry, Iam not getting u.

What don't you get? Passing another array to your function? Declaring another array? Pointers?

"c" do is declared, as local in the function "product"

Yes and that does not help him when he is trying to print the values from main.

Then how can I print this when I want to?

The same way you are doing now?

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.

When I declared array-C in main function and I passed it to product function{along with a & b }, I didn't get any error, but Iam getting wrong ans.

My 5. It takes patience. Sometimes I understand why you don't formally answer, just comment :-)

--SA

--SA

Thanks. Kind of like asking the same guy 3 times what version of VB he is using... :-)

Oh, did you notice?! And how was that? Do you think there is a hope for answer? :-)

--SA

--SA

Your main problem is you are declaring the function as float, but you are not returning anything. This is because C is locally declared.

Other point you have to consider is that you are passing two empty arrays to the function, because "a" and "b" have no values, you use them just behind the declaration.

Have a look to http://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm[^]. The example will show you what we mean. Hope it helps

Other point you have to consider is that you are passing two empty arrays to the function, because "a" and "b" have no values, you use them just behind the declaration.

Have a look to http://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm[^]. The example will show you what we mean. Hope it helps

v2

Comments

Sure, a 5.

--SA

--SA

Thank you

You should write something similar to:

[UPDATE]

These statements are**evil wrong** (at compile time

A working (although still ugly) version of your program:

[/UPDATE]

[UPDATE 2]

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:

[/UPDATE 2]

```
// a, b are IN parameters, while c is the OUT parameter
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); // c = a x b
//..
```

[UPDATE]

N Shiva wrote:float a[r1][c1];

float b[r2][c2];

These statements are

`r1`

, `c1`

, .. values are indefinite)!A working (although still ugly) version of your program:

```
/*Multiplication of two matrices*/
#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()
{
/*c1=r2*/
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)
{
/*scanning elements*/
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]);
}
}
/*scanning cmpltd*/
/*multiplication*/
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:

```
/*Multiplication of two matrices*/
#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;
/*c1=r2*/
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)
{
/*scanning elements*/
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]);
}
}
/*scanning cmpltd*/
/*multiplication*/
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]

v3

Comments

Sure, a 5.

You see, this time the question is, again, not quite correct (because formally this is not a question), but it does not affect validity of your advice. :-)

--SA

You see, this time the question is, again, not quite correct (because formally this is not a question), but it does not affect validity of your advice. :-)

--SA

Thank you.

I tried this, i mean product(a,b,c)..and all.....but i am not getting any error, but iam getting wrong ans. I donno why?

You should detail your scenario (since the program looks correct): please post your input, output (and expected output) data.

I defined functions like ... float product(float a[20][20],float b[20][20],float c[20][20])...

and called this function in main as product(a,b,c), (I declared array-'C' in main function)... then execution is as follows:

Enter no.of rows of first matrix: 2

Enter no.of columns of first matrix: 2

Enter no.of rows of second matrix: 2

Enter no.of columns of second matrix: 2

Enter the elements of first matrix: 1 2 3 4

Enter the elements of second matrix: 0 3 2 1

4.000000 5.000000

0.000000 -1.931809

But i shoud get

4 5

8 13

and called this function in main as product(a,b,c), (I declared array-'C' in main function)... then execution is as follows:

Enter no.of rows of first matrix: 2

Enter no.of columns of first matrix: 2

Enter no.of rows of second matrix: 2

Enter no.of columns of second matrix: 2

Enter the elements of first matrix: 1 2 3 4

Enter the elements of second matrix: 0 3 2 1

4.000000 5.000000

0.000000 -1.931809

But i shoud get

4 5

8 13

The code you have given is ok. But

1. if ( product(a,b,c) ) return -1;

this means exactly?

2. How can I use this product function when I wanted to calculate 'Q' which is equal to x*y*z.

1. if ( product(a,b,c) ) return -1;

this means exactly?

2. How can I use this product function when I wanted to calculate 'Q' which is equal to x*y*z.

(1) Dont worry too much about: just (rough) error handling (product returns 0 on success.

(2) You need a two steps approach:

product(a,b,t);

product(t,c,q);

where t[20][20] is a temporary array, q[20][20] is the result (array).

(2) You need a two steps approach:

product(a,b,t);

product(t,c,q);

where t[20][20] is a temporary array, q[20][20] is the result (array).

Thnx. I got it.

1. why these are wrong?

float a[r1][c1];

float b[r2][c2];

You said "These statements are evil wrong (at compile time r1, c1, .. values are indefinite)! "

2. I replaced if ( product(a,b,c) ) return -1; ...with... product(a,b,c). is there any mistake. i am getting correct result.

3. I donno why you changed definition of product function from float to integer.

1. why these are wrong?

float a[r1][c1];

float b[r2][c2];

You said "These statements are evil wrong (at compile time r1, c1, .. values are indefinite)! "

2. I replaced if ( product(a,b,c) ) return -1; ...with... product(a,b,c). is there any mistake. i am getting correct result.

3. I donno why you changed definition of product function from float to integer.

(1) Well actually some compilers accept those (as C99 extension). However, I believe you must use such a feature coherently.

(2) No, is not a mistake.

(3) Because the result is in the third parameter of the function (c[20][20]). Hence I used the return value to indicate whether the function was successfull or not.

(2) No, is not a mistake.

(3) Because the result is in the third parameter of the function (c[20][20]). Hence I used the return value to indicate whether the function was successfull or not.

Thnk you.. but array c is also float array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada
+1 416-849-8900 x 100

matr.c:5: error: variably modified ‘c’ at file scope

matr.c:5: error: variably modified ‘c’ at file scope

--SA