You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++多函数程序行列式计算结果异常:预期正值却输出负值

3x3行列式计算程序的符号错误问题

我为C++入门课程编写了两个程序:一个是仅包含main函数的单函数版本,另一个通过main调用多个拆分后的函数。两个程序使用完全相同的3x3行列式计算公式,但多函数版本的输出结果为负值,而正确结果应为正值。我检查过计算公式本身以及它的调用位置,没找到明显导致符号错误的原因。

后来我把多函数版本中getArray和outArray函数循环里的c和cols变量移除,替换成直接的下标值后,程序就输出了预期的正值结果。

多函数版本代码

#include<iostream>
#include<iomanip>
using namespace std; 

const int CINDEX=3;
const int RINDEX=3;
void getArray(float [][CINDEX]);
void outArray(const float [][CINDEX]);
float dtmntArray(const float[][CINDEX]);

int main()
{
    cout<<"Calculation of 3 x 3 Determinant\n\n";
    char yn;
    do{
    float fArray[RINDEX][CINDEX];
    getArray(fArray);       //get array input
    outArray(fArray);       //output array
    float d=dtmntArray(fArray);                     //get determinant
    cout<<"The determinant of the above matrix is "<<d<<endl;
    cout<<"\nWould you like to Do another Matrix (Y/N)? "; //loop
    cin>>yn;
    }while (yn!='N');
}

void getArray(float iArray[][CINDEX])
{
    int r, c;
    for (r=1;r<=RINDEX;r++) {
    c=0;
    cout<<"What is Row "<<r<<" of the matrix? ";
    cin>>iArray[r-1][c]>>iArray[r-1][c++]>>iArray[r-1][c++];
    }
    
    return;
}

void outArray (const float oArray[][CINDEX])
{
    cout<<"3 x 3 Matrix entered is\n";
    int rows, cols;
    for (rows=0;rows<RINDEX;rows++) {
        cols=0;
        cout<<fixed<<setprecision(3)<<left<<setw(7)<<
        oArray[rows][cols]<<setw(7)<<oArray[rows][cols++]<<
        setw(7)<<oArray[rows][cols++]<<endl;
    }
    
    return;
}

float dtmntArray (const float dArray [][CINDEX])
{
    float determ;
    determ=(dArray[0][0]*dArray[1][1]*dArray[2][2])+
    (dArray[0][1]*dArray[1][2]*dArray[2][0])+
    (dArray[0][2]*dArray[1][0]*dArray[2][1])-
    (dArray[0][1]*dArray[1][0]*dArray[2][2])-
    (dArray[0][0]*dArray[1][2]*dArray[2][1])-
    (dArray[0][2]*dArray[1][1]*dArray[2][0]);
    
    return determ;
}

单函数版本代码

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    char yn;
    do{
        //create array
        const int RINDEX=3;
        const int CINDEX=3;
        float mArray [RINDEX] [CINDEX];
        //entering values for matrix
        cout<<"Calculation of 3 x 3 Determinant\n\n";
        cout<<"What is Row 1 of the matrix? ";
        cin>>mArray[0][0]>>mArray[0][1]>>mArray[0][2];
        cout<<"What is Row 2 of the matrix? ";
        cin>>mArray[1][0]>>mArray[1][1]>>mArray[1][2];
        cout<<"What is Row 3 of the matrix? ";
        cin>>mArray[2][0]>>mArray[2][1]>>mArray[2][2];
        //output Matrix
        cout<<"\n\n3 x 3 Matrix entered is\n";
        cout<<fixed<<setprecision(3)<<left<<setw(7)<<mArray[0][0]<<
            setw(7)<<mArray[0][1]<<setw(7)<<mArray[0][2]<<endl;
        cout<<setw(7)<<mArray[1][0]<<setw(7)<<mArray[1][1]<<
            setw(7)<<mArray[1][2]<<endl;
        cout<<setw(7)<<mArray[2][0]<<setw(7)<<mArray[2][1]<<
            setw(7)<<mArray[2][2]<<endl;
        //determinant
        float determ;
        determ=(mArray[0][0]*mArray[1][1]*mArray[2][2])+
        (mArray[0][1]*mArray[1][2]*mArray[2][0])+
        (mArray[0][2]*mArray[1][0]*mArray[2][1])-
        (mArray[0][1]*mArray[1][0]*mArray[2][2])-
        (mArray[0][0]*mArray[1][2]*mArray[2][1])-
        (mArray[0][2]*mArray[1][1]*mArray[2][0]);
        cout<<"The determinant of the above matrix is "<<determ<<endl;
        //Loop it
        cout<<"\nWould you like to Do another Matrix (Y/N)? ";
        cin>>yn;
    } while (yn!='N');
}

内容的提问来源于stack exchange,提问作者HMD2020

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:02:02