Click here to Skip to main content
11,639,765 members (59,705 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Errors mathematics
/*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.)


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.
Posted 9-Oct-12 5:32am
N Shiva526
Edited 9-Oct-12 6:29am
Nelek72K
v3
Comments
N Shiva at 9-Oct-12 11:35am
   
Iam using array-c in main() even though it is not declared in main(). But even If I declare above main also Iam getting this error.

matr.c:5: error: variably modified ‘c’ at file scope
matr.c:5: error: variably modified ‘c’ at file scope
Sergey Alexandrovich Kryukov at 9-Oct-12 16:45pm
   
Matrix multiplication is one of the simplest things in calculations, but your problem is understanding of the very basics of programming and language. I suggest you learn more of it on more simple examples first.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
N Shiva at 9-Oct-12 11:47am
   
How can I catch the result in that function. I mean c[i][j].
I just want to store that in array c.
Wes Aday at 9-Oct-12 11:56am
   
Try declaring c in your main function and pass it to your product function. Might be easier to pass it as a pointer.
ridoy at 9-Oct-12 11:48am
   
right..+5
N Shiva at 9-Oct-12 12:06pm
   
Sorry, Iam not getting u.
Wes Aday at 9-Oct-12 12:14pm
   
What don't you get? Passing another array to your function? Declaring another array? Pointers?
Nelek at 9-Oct-12 12:11pm
   
"c" do is declared, as local in the function "product"
Wes Aday at 9-Oct-12 12:13pm
   
Yes and that does not help him when he is trying to print the values from main.
N Shiva at 9-Oct-12 12:13pm
   
Then how can I print this when I want to?
Wes Aday at 9-Oct-12 12:15pm
   
The same way you are doing now?
N Shiva at 9-Oct-12 12:39pm
   
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.
N Shiva at 9-Oct-12 12:17pm
   
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.
Sergey Alexandrovich Kryukov at 9-Oct-12 16:52pm
   
My 5. It takes patience. Sometimes I understand why you don't formally answer, just comment :-)
--SA
Wes Aday at 9-Oct-12 17:05pm
   
Thanks. Kind of like asking the same guy 3 times what version of VB he is using... :-)
Sergey Alexandrovich Kryukov at 10-Oct-12 2:02am
   
Oh, did you notice?! And how was that? Do you think there is a hope for answer? :-)
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 9-Oct-12 16:53pm
   
Sure, a 5.
--SA
Nelek at 9-Oct-12 18:10pm
   
Thank you
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You should write something similar to:
// 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 evil wrong (at compile time 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]
  Permalink  
v3
Comments
Sergey Alexandrovich Kryukov at 9-Oct-12 16:53pm
   
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
CPallini at 10-Oct-12 3:04am
   
Thank you.
N Shiva at 10-Oct-12 4:13am
   
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?
CPallini at 10-Oct-12 5:05am
   
You should detail your scenario (since the program looks correct): please post your input, output (and expected output) data.
N Shiva at 10-Oct-12 5:21am
   
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
N Shiva at 10-Oct-12 6:20am
   
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.
CPallini at 10-Oct-12 6:29am
   
(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).
N Shiva at 10-Oct-12 6:36am
   
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.
CPallini at 10-Oct-12 7:59am
   
(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.
N Shiva at 11-Oct-12 1:50am
   
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)

  Print Answers RSS
0 Suvendu Shekhar Giri 330
1 Sergey Alexandrovich Kryukov 204
2 CPallini 177
3 F-ES Sitecore 170
4 himanshu agarwal 167
0 OriginalGriff 9,081
1 Sergey Alexandrovich Kryukov 8,847
2 Mika Wendelius 7,027
3 Suvendu Shekhar Giri 2,650
4 F-ES Sitecore 2,558


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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