1
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。

这是输出的屏幕截图:似乎无法从截图工具中粘贴。但问题似乎是在“反向替换”开始后开始的。任何帮助,将不胜感激。

4

1 回答 1

0

我不确定所有问题是什么,但一个明显的问题是您的outMatrix. 您正在返回一个指向本地数组的指针。数组在函数结束时被销毁。您返回的指针现在是垃圾。

更改此行:

float outMatrix[DEFROWS] = {0.0};

至:

float* outMatrix = new float[DEFROWS];

然后你需要把它归零。

注意:如果你使用 a 并返回它会更好vector<float>,但是你用原始指针编写它,所以我用原始指针回答。原始指针会导致内存泄漏。应尽可能避免使用它们。使用现代 C++,很少需要分配原始指针。

编辑:见有效 C++ 项目 #31

EDIT2:更改为使用向量,如果您的编译器支持 C++11 标准中定义的移动运算符和构造函数,则返回向量是一种廉价的操作。

std::vector<float> solveMatrix(/* Your arguments */)
{

    std::vector<float> outMatrix(DEFROWS, 0.0);

    // Your code

    return outMatrix;
}
于 2012-06-28T20:40:50.467 回答