简体   繁体   中英

Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted

this error keeps coming up yet i dont see a problem with the code (its in c++) the program is supposed to find the inverse of a 2x2 matrix

#include <iostream>

using namespace std;

int main() { 
    float d;
    float A[2][2], B[2][2];

    do {
        cout << "please enter valid parameters in for 11,12,21,22" << endl;

        for(int i = 0; i < 2; i++) {
            for(int j = 0; j < 2; j++)
                cin >> A[i][j];
        }

        d = (A[1][1] * A[2][2]) - (A[1][2] * A[2][1]);
    } while(d == 0);

    B[1][1] = A[2][2] * (1.0 / d);
    B[1][2] = A[1][2] * (-1.0 / d);
    B[2][1] = A[2][1] * (-1.0 / d);
    B[2][2] = A[1][1] * (1.0 / d);

    for(int k = 0; k < 2; k++) {
        for(int h = 0; h < 2; h++) 
            cout << B[k][h] << " ";
        cout << endl;
    }

    return 0;
}

您将B和A从1索引到2,而不是从0到1使用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM