【问题标题】:I wrote a program to add two matrices but it's not working. Any suggestions for what's wrong?我写了一个程序来添加两个矩阵,但它不起作用。有什么问题有什么建议吗?
【发布时间】:2018-09-30 19:59:24
【问题描述】:

该程序从用户那里获取两个数组并将它们相加并打印总和。该程序使用嵌套的 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");
    }


}

【问题讨论】:

  • main() int main(void) 和int main(int argc, void **argv)。实现也可以免费支持其他原型,但简单的 main() 不太可能是正确的。
  • 除此之外,无需查看代码:它会帮助先自己进行一些调试。有一篇很好的博文 How to debug small programs 可以指导您。如果您仍然卡住,它还将帮助您创建一个minimal reproducible example,这将反过来帮助其他人快速了解具体问题并提供帮助。
  • @FelixPalmen 程序无法编译,因此此时调试指令将无济于事。
  • 编译器返回错误array1 not declared in this scope ? 因为array1 是在if(input==1) 块内声明的,而您在该范围外访问。
  • 很可能您的变量范围太窄,如果您遵循缩进代码的标准模式之一,这很明显。 (我使用 K & R 样式,但在 ){ 之间没有空格)。

标签: c for-loop matrix multidimensional-array nested-loops


【解决方案1】:

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

编译器返回错误 array1 not declared in this scope ? 因为 array1 是在 if(input==1) 块内声明的,而您正在该范围外访问。

同样声明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;
}

【讨论】:

  • 你为什么要求使用 main(void) 而不是 main()
  • C 标准这么说。程序启动时调用的函数名为main。实现没有声明这个函数的原型。它应使用int 的返回类型和无参数定义:int main(void) { /* ... */ } 或使用two 参数(此处称为 argc 和 argv,尽管可以使用任何名称,因为它们对于它们被声明):int main(int argc, char *argv[]) { /* ... */ }
  • 始终使用-Wall 标志编译您的程序,并且不要忽略编译器警告,例如gcc -Wall test.c
  • 我使用的是 IDE Dev C++
【解决方案2】:

您在块中声明 array1,以:

if(input==1)

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

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

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

【讨论】:

    猜你喜欢
    • 2021-05-26
    • 2020-08-01
    • 2021-06-14
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2020-11-04
    • 2014-08-28
    相关资源
    最近更新 更多