在C+&#x2B中以可变大小的矩阵输入输入。

发布于 01-25 16:10 字数 1337 浏览 3 评论 0原文

我正在尝试检查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]同样的是功能中的其他参数,但随后我得到了错误,

"expression must have a constant value"
"array bound is not a integer constant before "]" token */

请建议我做什么,以使这些功能正常工作

I am trying to check the orthogonality of a matrix A in c++.
For that I am declaring a "MAX" value of number of rows and columns, then I am trying to get the number of rows and columns for the matrix, hence declaring it.
After that I am trying to get the input for the matrix elements by using pointer to the rows of the matrix and so on ..

#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);
    }
}

When compiling it I am getting the error

cannot convert 'float (*)[acolumns] to float (*)[10]' 

I have tried replacing line 4 with

extern int arows,acolumns;

and replacing float (*arr)[MAX] by
float arr[][acolumns] similarly other parameters in the functions but then I get the error that

"expression must have a constant value"
"array bound is not a integer constant before "]" token */

Please suggest me what should I do, so as to make these functions work properly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦萦几度2025-02-01 16:10:37

根本原因是不允许使用C ++ VLA(Varibale Length Arrays)。到目前为止,它不是语言C ++的一部分。

float a [Arwows] [Acolumns];

是无效的C ++代码。

在C中您可以做,但不能在C ++中

The root cause is that in C++ VLA (Varibale Length Arrays) are not allowed. It is not a part of the language C++ so far.

float A[arows][acolumns];

is invalid C++ code.

In C you can do, but not in C++

楠木可依2025-02-01 16:10:37

在C中,您不能在程序期间创建双索引数组或更改其大小。
因此,是的,您必须告诉程序在编译之前的大小是什么。

您可以使用其最大大小创建此数组,并在功能中以其在功能中发送“ Arws”和“ Acolumns”,仅在“二手空间”上使用。

In C, you can't create double index array or change its size during the program.
So yes, you have to tell the programme what's the size of A before compiling.

You can create this array with its max size and send "arows" and "acolumns" with it in your functions to work only on "used space" instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文