C++ Gauss-Seidel法计算3个线性方程组

C++ Gauss-Seidel法计算3个线性方程组,c++,linear-algebra,C++,Linear Algebra,我的任务是开发一个程序来计算3个线性方程组:该程序必须允许用户输入系数和常数、迭代次数和可接受误差水平。我似乎不能将迭代次数和错误级别都作为参数来停止循环并显示变量的最终值。以下是我目前掌握的情况: #include <iostream> #include <windows.h> using namespace std; int main() { cout<<"Welcome. This is Problem 1. "<<endl;

我的任务是开发一个程序来计算3个线性方程组:该程序必须允许用户输入系数和常数、迭代次数和可接受误差水平。我似乎不能将迭代次数和错误级别都作为参数来停止循环并显示变量的最终值。以下是我目前掌握的情况:

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    cout<<"Welcome. This is  Problem 1. "<<endl;
    cout<<"computing systems of  three linear equations through gauss-seidel method"<<endl;

    float coefEqxn1[3];
    for (int x=0; x<3;)
    {
        for ( int eq1=1; eq1<=3; eq1++)
        {
            cout<<"Please enter Coefficient " <<eq1<< " of equation 1 : ";
            cin>>coefEqxn1[x];
            x++;
        }
    }

    float coefEqxn2[3];
    for (int x=0; x<3;)
    {
        for ( int eq2=1; eq2<=3; eq2++)
        {
            cout<<"Please enter Coefficient " <<eq2<<" of equation 2 :" ;
            cin>>coefEqxn2[x];
            x++;
        }
    }

    float coefEqxn3[3];
    for (int x=0; x<3;)
    {
        for ( int eq3=1; eq3<=3; eq3++)
        {
            cout<<"Please enter Coefficient "<<eq3<<" of equation 3 :";
            cin>>coefEqxn3[x];
            x++;
        }
    }

    float constants[3];
    for (int y=0; y<3;)
    {
        for (int con=1; con<=3; con++)
        {
            cout<<"Please enter the contant of equation "<<con<<" : ";
            cin>>constants[y];
            y++;
        }
    }

    cout<<"Calculating through Cramer's Rule..."<<endl;

    int iteration=0;
    cout<<"enter # iteration"<<endl;
    cin>>iteration;

    int stopC=0;
    cout<<"enter level of error"<<endl;
    cin>>stopC;

    float matrixArray[3][4];
    {
        for ( int y=0; y<3;)
        {
            for (int x=0; x<=3;x++)
                matrixArray[0][y]=coefEqxn1[y];
            y++;
        }

        matrixArray[0][3]=constants[0];

        for ( int y=0; y<3;)
        {
            for (int x=0; x<=3;x++)
                matrixArray[1][y]=coefEqxn2[y];
            y++;
        }

        matrixArray[1][3]=constants[1];
        for ( int y=0; y<3;)
        {
            for (int x=0; x<=3;x++)
                matrixArray[2][y]=coefEqxn3[y];
            y++;
        }

        matrixArray[2][3]=constants[2];
    }

    for(int a=0; a<3; a++)
    {
        for(int b=0; b<=3; b++)
            cout<<"matrixArray["<<a<<"]["<<b<<"]: "<<matrixArray[a][b]<<endl;   
    }

    float valueOfX[100], valueOfY[100], valueOfZ[100];

    for( int i=1; i<iteration; )
    {
        valueOfX[0]=0, valueOfY[0]=0, valueOfZ[0]=0;

        valueOfX[i]=(matrixArray[0][3]-(matrixArray[0][2]*valueOfZ[i-1]+matrixArray[0][1]*valueOfY[i-1]))/matrixArray[0][0];
        valueOfY[i]=(matrixArray[1][3]-(matrixArray[1][2]*valueOfZ[i-1]+matrixArray[1][0]*valueOfX[i]))/matrixArray[1][1];
        valueOfZ[i]=(matrixArray[2][3]-(matrixArray[2][1]*valueOfY[i]+matrixArray[2][0]*valueOfX[i]))/matrixArray[2][2];

        float reX=0, reY=0, reZ=0;

        reX=((valueOfX[i+1]-valueOfX[i])/valueOfX[i+1])*100;
        reY=((valueOfY[i+1]-valueOfY[i])/valueOfY[i+1])*100;
        reX=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;

        if (reX<=inputErrorLevel)
            break;

        if (reY<=inputErrorLevel)
            break;

        if (reZ<=inputErrorLevel)
            break;

        cout<<"reX = "<<reX<<endl;
        cout<<"reY = "<<reY<<endl;
        cout<<"reY = "<<reX<<endl;

        i++;
    }

    cout<<"x = "<<valueOfX[iteration-1]<<endl;
    cout<<"y = "<<valueOfY[iteration-1]<<endl;
    cout<<"z = "<<valueOfZ[iteration-1]<<endl;
}
#包括
#包括
使用名称空间std;
int main()
{

根据你想要的,你应该有

double inputErrorLevel; //Read from user input

// The for loop guarantees that the loop will run at max iteration times.
// Noticed I changed it to start from zero otherwise it will only run
// iteration -1 times 
for( int i=0; i<iteration; ++i )
{

  //Do heavy computation stuff.

  double currentErrorLevel = ComputeErrorAtCurrentIteration();
  if ( currentErrorLevel < inputErrorLevel )
       break; // break will ensure more iterations are not done
              // if error level is acceptable
}
double-inputErrorLevel;//从用户输入读取
//for循环保证循环将以最大迭代次数运行。
//注意到我把它改为从零开始,否则它只会运行
//迭代-1次

对于(inti=0;i这看起来像是一个打字错误

reX=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;
这不应该是:

reZ=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;
这里也一样:

cout<<"reY = "<<reX<<endl;

这里有三个变量,所以我必须计算每个变量吗?
reZ=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;
cout<<"reY = "<<reX<<endl;
cout<<"x = "<<valueOfX[i-1]<<endl;
cout<<"y = "<<valueOfY[i-1]<<endl;
cout<<"z = "<<valueOfZ[i-1]<<endl;