Click here to Skip to main content
15,886,873 members

Please clarify my doubt on matrix multiplication

N Shiva asked:

Open original thread
/*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.
Tags: C, Errors, Mathematics

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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