c++ - C++ 中的嵌套循环和矩阵

标签 c++ nested-loops

我一直在尝试编写一个 C++ 程序来执行 2 个矩阵的加法,这就是代码 我已经写了。但我一直收到错误消息“进程返回 -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;
    }

最佳答案

在求和逻辑中,嵌套迭代器变量 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开始

关于c++ - C++ 中的嵌套循环和矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60817195/

相关文章:

ruby - 经验丰富的 Ruby 程序员将如何重写我的迷你游戏?

java - 如何使用嵌套循环每次打印字符串时取出字符串的最后一个字符

java - 嵌套循环产生特定输出

algorithm - 嵌套for循环的Big-O : Linear or Quadratic?

c++ - 如何在 C++ 中同时使用默认和自定义复制构造函数?

c++ - 查找未排序数组的模式,以及该数组是否具有多个模式或没有模式

C++:扩展模板类

c++ - 无法从 NULL 资源创建 RenderTargetView

c++ - 在 Vector 中查找值的索引,最接近输入

c++ - 有人可以为我解释这个函数的最后一个参数吗?