|
|
#include<iostream.h>
/* This function uses Gauss-elimination method
to solve multi-variable */
void Gauss(double *A, /* A is nXn order matrix */
double *b, /* b is nX1 order matrix */
double *x, /* x is nX1 order matrix */
int n /* row and column of A */
)
{
int i,j,k; /* indices */
double temp;
for(i=0;i<n;i++) /* convert A to top-triangle matrix */
{
for(k=i+1;k<n;k++)
for(j=i;j<n;j++)
{
temp=A[k]/(A);
A[k][j]=(A[k][j])-temp*(A[j]);
b[k]=b[k]-temp*b;
}
}
for(i=n-1;i>=0;i--) /* get the solution of x */
{
if(i-n-1) x=b/A;
else
{
for(j=i+1;j<n;j++)
temp+=A[j]*x[j];
x=(b-temp)/A;
}
}
}
int main() /* main part of program */
{
int n; /* row and column of A or row of b */
int i,j; /* indexes */
cout<<"请输入n:";
cin>>n;
double A[n][n],b[n],x[n],(*pa)[n],*pb,*px; /* define matrixes */
pa=A;
pb=b;
px=x;
cout<<"请输入A:\n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>A[j];
cout<<"请输入b:"<<endl;
for(i=0;i<n;i++)
cin>>b;
Gauss(pa,pb,px,n);
for(i=0;i<n;i++)
cout<<x<<endl;
} |
|