请澄清我对矩阵乘法的怀疑 [英] Please clarify my doubt on matrix multiplication

查看:107
本文介绍了请澄清我对矩阵乘法的怀疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/*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");
}


该程序将两个矩阵作为输入并给出其乘积.当我编写了没有功能的程序时,我得到了解决方案.我想在其他程序中使用此操作,这就是为什么我需要将此作为函数编写的原因.但是我遇到了这个错误.

提前thnx.


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.)




OP的重要信息是从下面的注释中复制的:
我编写此函数的唯一意图是,在与图论相关的程序中使用它.




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.

推荐答案

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



您尚未在本声明中的任何地方声明"c".要在main中使用c,必须像对a和b一样进行声明.



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.


您的主要问题是将函数声明为float,但未返回任何内容.这是因为C是在本地声明的.

您还需要考虑的另一点是,您要将两个空数组传递给该函数,因为"a"和"b"没有值,因此可以在声明后面使用它们.

看看 http://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm [ ^ ].该示例将向您展示我们的意思.希望对您有帮助
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


您应该编写类似以下内容的内容:
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])
{
//... 
}


然后在main函数中,


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写道:
N Shiva wrote:

float a [r1] [c1];
float b [r2] [c2];

float a[r1][c1];
float b[r2][c2];


这些语句是 错误的 (在编译时r1c1,..值是不确定的)!

程序的有效版本(尽管仍然很难看):


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]



[/UPDATE]

[UPDATE 2]

N Shiva写道:
N Shiva wrote:

float a [r1] [c1];
浮动 b [r2] [c2];

float a[r1][c1];
float b[r2][c2];


实际上(我不知道),默认情况下,某些编译器(例如GCC)实现了此C99扩展名(


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]



[/UPDATE 2]


这篇关于请澄清我对矩阵乘法的怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆