我写了一个程序来添加两个矩阵,但它不工作。有什么问题的建议吗?

I wrote a program to add two matrices but it's not working. Any suggestions for what's wrong?

此程序从用户那里获取两个数组并将它们相加并打印总和。该程序使用嵌套的 for 循环来获取值并打印值。编译器返回错误 array1 未在此范围内声明。如果我删除总和打印部分,程序也会停止工作。任何缩短程序的建议表示赞赏。

#include<stdio.h>

int a,b;

int i,j;

main()
{

    printf("Enter the size of the array  \n Rows : ");
    scanf("%d",&a);
    printf("Columns : ");
    scanf("%d",&b);

    int array[a][b];

    printf("Enter the values of the %dx%d array : \n",a,b);

    for(i=0;i<a;i++)
    {
        for(j=0;j<b;j++)
        {
            scanf("%d",&array[i][j]);
        }
    }

    printf("The values of the First Matrix are :\n");


    for(i=0;i<a;i++)
    {
        for(j=0;j<b;j++)
        {
            printf("%d\t",array[i][j]);
        }

        printf("\n");
    }

    int input;
    printf("If you want to do further operations on Matrices press 1\n");
    scanf("%d",&input);

    if(input==1)
    {
        printf("Enter the size of the array  \n Rows : ");
    scanf("%d",&a);
    printf("Columns : ");
    scanf("%d",&b);

    int array1[a][b];

    printf("Enter the values of the %dx%d array : \n",a,b);

    for(i=0;i<a;i++)
    {
        for(j=0;j<b;j++)
        {
            scanf("%d",&array1[i][j]);
        }
    }

    printf("The values of the Second Matrix are :\n");


    for(i=0;i<a;i++)
    {
        for(j=0;j<b;j++)
        {
            printf("%d\t",array1[i][j]);
        }

        printf("\n");
    }

    }


    input = 0;
    printf("If you want to add the two matrices press 1 \n");
    scanf("%d",input);

    int array2[a][b];

    if(input==1)
    {
        for(i=0;i<a;i++)
    {
        for(j=0;j<b;j++)
        {
            array2[i][j] =  array[i][j]+array1[i][j];
        }
    }

    }

    printf("The Sum of the first and Second array is : \n ");


    for(i=0;i<a;i++)
    {
        for(j=0;j<b;j++)
        {
            printf("%d\t",array2[i][j]);
        }

        printf("\n");
    }


}

您在以以下开头的块中声明了 array1:

if(input==1)

然后您尝试在该块之外使用它,这会导致编译器错误。

array2[i][j] =  array[i][j]+array1[i][j];

最快的解决方案是将块后面的代码嵌套在其中,这样 array1 就在范围内。

你的代码有很多问题,建议你正确使用花括号{..}。也使用 int main(void) { } 而不是 main() { }.

编译器返回错误 array1 not declared in this scope ? Because array1 is declared inside if(input==1) block and you are accessing outside the scope .

另外语句 scanf("%d",input); 是错误的,它给出了警告,用 -Wall 标志编译你的程序。

最后避免为这个小任务使用全局变量或使用 #define 定义 rowcolumn 值。

这里是修改后的代码

int main() {
        printf("Enter the size of the array  \n Rows : ");
        int a = 0,b = 0;
        scanf("%d",&a);
        printf("Columns : ");
        scanf("%d",&b);
        int array[a][b];
        printf("Enter the values of the %dx%d array : \n",a,b);

        for(int i=0;i<a;i++) {
                for(int j=0;j<b;j++) {
                        scanf("%d",&array[i][j]);
                }
        }
        printf("The values of the First Matrix are :\n");
        for(int i=0;i<a;i++) {
                for(int j=0;j<b;j++) {
                        printf("%d\t",array[i][j]);
                }
                printf("\n");
        }
        int input;
        printf("If you want to do further operations on Matrices press 1\n");
        scanf("%d",&input);
        if(input==1) {
                printf("Enter the size of the array  \n Rows : ");
                scanf("%d",&a);
                printf("Columns : ");
                scanf("%d",&b);
                int array1[a][b];
                printf("Enter the values of the %dx%d array : \n",a,b);
                for(int i=0;i<a;i++) {
                        for(int j=0;j<b;j++) {
                                scanf("%d",&array1[i][j]);
                        }
                }
                printf("The values of the Second Matrix are :\n");
                for(int i=0;i<a;i++) {
                        for(int j=0;j<b;j++) {
                                printf("%d\t",array1[i][j]);
                        }
                        printf("\n");
                }

                input = 0;
                printf("If you want to add the two matrices press 1 \n");
                scanf("%d",&input);/* use &input */
                int array2[a][b];
                if(input==1) {
                        for(int i=0;i<a;i++) {
                                for(int j=0;j<b;j++) {
                                        array2[i][j] =  array[i][j]+array1[i][j];
                                }
                        }
                }
                printf("The Sum of the first and Second array is : \n ");
                for(int i=0;i<a;i++) {
                        for(int j=0;j<b;j++) {
                                printf("%d\t",array2[i][j]);
                        }
                        printf("\n");
                }
        }
        return 0;
}