【问题标题】:Nested loops and matrices in C++C++ 中的嵌套循环和矩阵
【发布时间】:2020-03-23 16:03:23
【问题描述】:

我一直在尝试编写一个用于添加 2 个矩阵的 c++ 程序,这是代码 我已经写了。但是我一直有错误说“进程返回-1073741819(0xC0000005)”你能帮我找出我的错误吗?

    int main()
    {
        float a[3][3],b[3][3],c[3][3];
        int l,k;

        cout<<"tell me the nr of lines in the vectors"<<endl;
        cin>>l;
        cout<<"tell me the nr of columns in the vectors"<<endl;
        cin>>k;
        for(int i=1;i<=l;i++){
            for(int j=1;j<=k;j++){
                cout<<"A["<<i<<"]"<<"["<<j<<"]= ";
                cin>>a[i][j];
            }
        }
        for(int i=1;i<=l;i++){
            for(int j=1;j<=k;j++){
                cout<<"B["<<i<<"]"<<"["<<j<<"]= ";
                cin>>b[i][j];
            }
        }
        for(int i=1; i<=l;i++){
            for(int j=1;j<=k;i++){
                c[i][j]=a[i][j]+b[i][j];
            }
        }
        cout<<"the sum of matrices A & B is;"<<endl;

/* i have also added this code here instead of the following loop just to see if there was a problem with the addition procedure or displayin the results;
cout<<c[i][j];   */

        for(int i=1;i<=l;i++){
            for(int j=1;j<=k;j++){
                cout<<c[i][j];
            }
        }

    return 0;
    }

【问题讨论】:

  • @Eyup Yilmaz C++ 中的数组索引从 0 开始。
  • @dan 我应该将其添加为 using namespace std::array<:array>, 3> 还是如何添加?
  • @VladfromMoscow 我试过了,但没有任何改变。
  • 0xC0000005 是访问冲突。这意味着您的代码正在尝试访问它不拥有的内存。
  • @Eyup Yilmaz 还有这些语句 cout>l; cout>k;没有意义,因为没有检查用户是否输入了正确的值。

标签: c++ nested-loops


【解决方案1】:

在您的求和逻辑中,嵌套迭代器变量j 没有递增,而是i。 看起来像:

for(int i=1; i<=l;i++){
            for(int j=1;j<=k;i++){ /*Change i to j*/ 
                c[i][j]=a[i][j]+b[i][j];
            }
        }

所以,它看起来像:

for(int i=1; i<=l;i++){
        for(int j=1;j<=k;j++){
            c[i][j]=a[i][j]+b[i][j];
            }
    }

整个代码变成:

    #include <iostream>
    using namespace std;

    int main()
    {
        float a[3][3], b[3][3], c[3][3];
        int l, k;

        cout << "tell me the nr of lines in the vectors" << endl;
        cin >> l;
        cout << "tell me the nr of columns in the vectors" << endl;
        cin >> k;
        for (int i = 0; i < l; i++)
        {
            for (int j = 0; j < k; j++)
            {
                cout << "A[" << i << "]"
                    << "[" << j << "]= ";
                cin >> a[i][j];
            }
        }
        for (int i = 0; i < l; i++)
        {
            for (int j = 0; j < k; j++)
            {
                cout << "B[" << i << "]"
                    << "[" << j << "]= ";
                cin >> b[i][j];
            }
        }
        for (int i = 0; i < l; i++)
        {
            for (int j = 0; j < k; j++)
            {
                c[i][j] = a[i][j] + b[i][j];
            }
        }
        cout << "the sum of matrices A & B is;" << endl;

        /* i have also added this code here instead of the following loop just to see if there was a problem with the addition procedure or displayin the results;
    cout<<c[i][j];   */

        for (int i = 0; i < l; i++)
        {
            for (int j = 0; j < k; j++)
            {
                cout << c[i][j];
            }
        }

        return 0;
    }

最终输出:

tell me the nr of lines in the vectors
2
tell me the nr of columns in the vectors
3
A[1][1]= 1
A[1][2]= 2
A[1][3]= 3
A[2][1]= 4
A[2][2]= 5
A[2][3]= 6
B[1][1]= 1
B[1][2]= 2
B[1][3]= 3
B[2][1]= 4
B[2][2]= 5
B[2][3]= 6
the sum of matrices A & B is;
24681012
Process finished with exit code 0

PS:不要从1开始索引,从0开始

【讨论】:

  • 非常感谢。你能告诉我为什么从 0 开始索引很重要,如果它在我从 1 开始时有效吗?
  • @drescherjm 只是想指出 OP 到底在哪里犯了错误。
  • 为什么从 0 开始索引很重要 因为数组在 c++ 中被索引为 0 .. size-1 使用 1 .. size 是未定义的行为,使您的整个程序未定义。当你有 UB 时,程序的结果是没有意义的。
  • 用 0 索引的原因是数组通常可以用指针索引,即 int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; 当你写 arr[0] 它通常意味着 *(arr + 0) 除非你在 Matlab 中并且不想有未定义的行为总是使用 0 进行索引。
  • 如果它在我从 1 开始时有效? 实际上没有。 Undefined Behavior 最糟糕的行为之一是损坏的代码(违反语言规则的代码)似乎给了您预期的结果。这会给您一种错误的感觉,即当程序损坏时程序运行正常,并且在您重新编译、更新编译器、房间温度发生变化时可能会失败……https://en.cppreference.com/w/cpp/language/ub 在这里您访问的范围超出了在语言规则中建立的数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多