这个错误不断出现,但我没有看到代码有问题(它在 C++ 中) 该程序应该找到 2x2 矩阵的逆矩阵
#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;
}
最佳答案
您正在从 1 到 2 索引 B 和 A,而不是从 0 到 1 使用它。
关于c++ - 运行时检查失败 #2 - 变量 'B' 周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10634573/