请大家帮我看一下这个程序为什么不对?(高斯列主元消去法解方程组)
#include<iostream>#include<math.h>
using namespace std;
const int N=9;
double X[N],Y[N]={-15,27,-23,0,-20,12,-7,7,10};
double A[N][N]={{31,-13,0,0,0,-10,0,0,0},{-13,35,-9,0,-11,0,0,0,0},
{0,-9,31,-10,0,0,0,0,0},{0,0,-10,79,-30,0,0,0,-9},
{0,0,0,-30,57,-7,0,-5,0},{0,0,0,0,-7,47,-30,0,0},
{0,0,0,0,0,-30,41,0,0},{0,0,0,0,-5,0,0,27,-2},
{0,0,0,-9,0,0,0,-2,29}};
void SortFun() //选主元,化成三角矩阵。
{
double ta,tb,m;
int max,i,k,j;
for(k=0;k<N;k++)
{
max=k;
for(i=k+1;i<N;i++)
{
if(fabs(A[i][k])>fabs(A[max][k]))
max=i;
}
if(i!=k)
{
for(j=0;j<N;j++)
{
ta=A[k][j];A[k][j]=A[max][j];A[max][j]=ta;
}
tb=Y[k];Y[k]=Y[max];Y[max]=tb;
}
for(i=k+1;i<N;i++)
{
m=A[i][k]/A[k][k];
for(j=k;j<N;j++)
A[i][j]=A[i][j]-A[k][j]*m;
Y[i]=Y[i]-m*Y[k];
}
}
return;
}
void FigurFun() //求解过程
{
X[N-1]=Y[N-1]/A[N-1][N-1];
double sum=0;
for(int i=N-2;i>=0;i--)
{
for(int j=i+1;j<N;j++)
{
sum+=A[i][j]*X[j];
}
X[i]=(Y[i]-sum)/A[i][i];
}
return;
}
void main(void)
{
SortFun();
FigurFun();
cout << "The final A is:" << endl;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
cout << A[i][j];
cout << " ";
if((j+1)%9==0)
cout << endl;
}
cout << "The final Y is:" << endl;
for(int i=0;i<N;i++)
{
cout << " ";
cout << Y[i];
}
cout << endl << "The root is:" << endl;
for(int i=0;i<N;i++)
{
cout << X[i] << " ";
if((i+1)%3==0) cout << endl;
}
}