源程序:
#include<stdio.h>
#include<math.h>
#define MAX_N 20
void main()
{ double a[MAX_N][MAX_N];
double b[MAX_N];
double x[MAX_N];
double t;
double s;
int n;
int i;
int j;
int k;
printf("Please input the dimension of the matrix;");//输入方程组AX=B的维数
scanf("%d",&n);
printf("Now input the coefficient matrix a(i,j),i,j=1,...,%d:/n",n);//输入系数A矩阵
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%lf",&a[i][j]);
printf("Now input the matrix b(i),i=1,...,%d:/n",n);//输入b矩阵
for(i=1;i<=n;i++)
scanf("%lf",&b[i]);
for(k=1;k<=n-1;k++)//消元过程
{for(i=k+1;i<=n;i++)
{t=a[i][k]/a[k][k];
for(j=k+1;j<=n;j++)
{a[i][j]=a[i][j]-t*a[k][j];}
b[i]=b[i]-t*b[k];
}
}
for(i=n;i>0;i--)
{s=b[i];
for(j=i+1;j<=n;j++)
s=s-a[i][j]*x[j];
x[i]=s/a[i][i];
}
for(i=1;i<=n;i++)
printf("x[%d]=%lf/n",i,x[i]);
输入提示:
在输入系数矩阵时,先输入维数
输入矩阵,一行一行地输入