我试图在c++中检验矩阵A的正交性。为此,我要声明行数和列数的“最大值”值,然后尝试获取矩阵的行数和列数,从而声明它。之后,我试图通过使用指向矩阵行的指针来获取矩阵元素的输入,等等。
#include <stdio.h>
#define MAX 10
int arows,acolumns; /*line 4*/
void input_matix(float (*arr)[MAX],int arows,int acolumns)
{/*asks user for input of individual elements*/}
void transpose(float (*T)[MAX],float (*A)[MAX],int arows,int acolumns)
{/*gets me the transpose*/}
void product(float (*A)[MAX],float (*T)[MAX],float (*P)[MAX])
{/*multiplies two matrices and places the product in P matrix*/}
int orthogonal_array(float (*arr)[MAX]){/*returns 1 if arr is ortho*/}
int main()
{
//int arows,acolumns;
printf("Enter the no. of rows and columns of matrix A: \n");
scanf("%d %d",&arows,&acolumns);
float A[arows][acolumns];
if (arows != acolumns)
{
printf("Not a orthogonal matrix.\n");
}
else
{
input_matix(A,arows,acolumns);
orthogonal_array(A);
}
}
在编译它时,我会得到错误
cannot convert 'float (*)[acolumns] to float (*)[10]'
我试过用
extern int arows,acolumns;
并用float arr[][acolumns]
替换浮点数(*arr)MAX,类似地,函数中的其他参数也是这样,但是我得到了以下错误:
"expression must have a constant value"
"array bound is not a integer constant before "]" token */
请建议我应该做些什么,以便使这些功能正常工作。
发布于 2022-04-30 06:46:24
根本原因是在C++中,VLA (Varibale Length数组)是不允许的。到目前为止,它还不是C++语言的一部分。
float A[arows][acolumns];
是无效的C++代码。
在C语言中,您可以这样做,但在C++中不行
发布于 2022-04-30 05:49:27
在C中,您不能在程序期间创建双索引数组或更改其大小。所以是的,你必须在编译之前告诉节目A的大小。
您可以创建这个数组的最大大小,并在您的函数中发送"arows“和"acolumns”,只在“二手空间”上工作。
https://stackoverflow.com/questions/72068945
复制相似问题