Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
/*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.

VB
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
Updated 9-Oct-12 6:29am
v3
Comments
N Shiva 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 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

C#
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.
 
Share this answer
 
Comments
N Shiva 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.
[no name] 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 9-Oct-12 11:48am    
right..+5
N Shiva 9-Oct-12 12:06pm    
Sorry, Iam not getting u.
[no name] 9-Oct-12 12:14pm    
What don't you get? Passing another array to your function? Declaring another array? Pointers?
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
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-Oct-12 16:53pm    
Sure, a 5.
--SA
Nelek 9-Oct-12 18:10pm    
Thank you
You should write something similar to:
C
// 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:
C
//..
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:
C
/*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:

C
/*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]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 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 10-Oct-12 3:04am    
Thank you.
N Shiva 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 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 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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900