Вложенные циклы и матрицы в C ++ - PullRequest
2 голосов
/ 23 марта 2020

Я пытался написать программу на 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;
    }

1 Ответ

4 голосов
/ 23 марта 2020

В вашей логике сумм c вложенная переменная итератора 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

...