float aMatrix[10][11];
float bMatrix[10];
// 这样调用...
solveMatrix(aMatrix, bMatrix, actualCol);
float* solveMatrix(float aMatrix[][DEFCOLS+1],float bMatrix[DEFCOLS], size_t cols){
std::cout << "\nInside solveMatrix...: " << endl;
size_t N2 = cols;
std::cout << "\N2 is...: " << N2 << endl;
for(size_t p=0; p<N2; p++){
//std::cout << "\nInside 1st for loop...: " << endl;
// find pivot row and swap
int max = p;
for(size_t i=p+1; i<N2; i++){
//std::cout << "\nInside 2nd for loop...: " << endl;
if ( abs(aMatrix[i][p]) > abs(aMatrix[max][p]) ){
max = i;
}
}
//std::cout << "\nJust b4 all the swapping...: " << endl;
float temp[] = { *aMatrix[p] };
*aMatrix[p] = *aMatrix[max];
*aMatrix[max] = *temp;
float t = bMatrix[p];
bMatrix[p] = bMatrix[max];
bMatrix[max] = t;
//std::cout << "\nDone all the swapping...: " << endl;
if ( abs(aMatrix[p][p]) <= MINISCULE) {
//std::cout << "***** Error matrix value too small. Matrix is singular" << endl;
//exit;
}
//std::cout << "\nJust the pivoting...: " << endl;
// Pivot /in A and b
for(size_t i=p+1; i<N2; i++){
//std::cout << "\nInside the 1st pivoting loop...: " << endl;
//std::cout << "\nAbout to do the [aMatrix[p][p]] division in back subst..: " << endl;
float alpha = aMatrix[i][p] / aMatrix[p][p];
bMatrix[i] = alpha * bMatrix[p];
for(size_t j=p; j<N2; j++){
//std::cout << "\nInside the 2nd pivoting loop...: " << endl;
aMatrix[i][j] -= alpha * aMatrix[p][j];
}
}
std::cout << "\nAbout to do the back subst..: " << endl;
// back subst.
float outMatrix[DEFROWS] = {0.0};
for(size_t i=N2-1; i>=0; i--){
std::cout << "\nInside the 1st back subst for loop..: " << endl;
float sum = 0.0;
for(size_t j=i+1; j<N2; j++){
std::cout << "\nInside the 2nd back subst for loop..: " << endl;
sum += aMatrix[i][j] * outMatrix[j];
}
std::cout << "\nAbout to do the [aMatrix[i][i]] division in back subst..: " << endl;
std::cout << "\n*outMatrix[i]: " << outMatrix[i] << endl;
std::cout << "\n( bMatrix[i] - sum ) : " << ( bMatrix[i] - sum ) << endl;
std::cout << "\n****aMatrix[i][i] : " << aMatrix[i][i] << endl;
if (aMatrix[i][i] > 0){
std::cout << "\nDid the division [aMatrix[i][i]] > 0 division in back subst..: " << endl;
std::cout << "\n*outMatrix[i]: " << outMatrix[i] << endl;
std::cout << "\n( bMatrix[i] - sum ) : " << ( bMatrix[i] - sum ) << endl;
std::cout << "\naMatrix[i][i] : " << aMatrix[i][i] << endl;
outMatrix[i] = ( bMatrix[i] - sum ) / aMatrix[i][i];
std::cout << "\nDid the division [aMatrix[i][i]] > 0 division in back subst..DONE: " << endl;
}else {
std::cout << "\nDid the divirion [aMatrix[i][i]] = 0 division in back subst..: " << endl;
outMatrix[i] = 0.0;
std::cout << "\nDid the divirion [aMatrix[i][i]] = 0 division in back subst..DONE: " << endl;
}
std::cout << "\nDid the [aMatrix[i][i]] division in back subst..: " << endl;
}
std::cout << "\nLeft the back subst for loops..: " << endl;
return outMatrix;
}
} // end solveMatrix()
我的问题是我的程序似乎在运行,但似乎超过了矩阵的末尾并崩溃了。另外,我得到了一些大的指数数字,数组中的最大数字是 1-10。
这是输出的屏幕截图:似乎无法从截图工具中粘贴。但问题似乎是在“反向替换”开始后开始的。任何帮助,将不胜感激。