|

楼主 |
发表于 2024-4-15 23:12:22
|
显示全部楼层
#include <stdio.h>
#define N 6
//声明高斯消元函数
void gaussian_elimination(float a[N][N+1]);
int main(){
//设定矩阵系数与常数项
float a[N][N+1]={
{0,0,0,0,0,1,1},
{0,0,0,0,1,0,1},
{0,0,0,1,0,0,1},
{1,1,1,1,1,1,6},
{5,4,3,2,1,0,15},
{20,12,6,2,0,0,40},
};
gaussian_elimination(a);
//代入高斯消元函数
return 0;
}
void gaussian_elimination(float a[N][N+1]){
int i,j,k;
float factor;
for(k=0;k<N;k++){
for(i=k+1;i<N;i++){
factor = a[i][k]/a[k][k];
for(j=k;j<N+1;j++){
a[i][j] -= factor*a[k][j];
}
}
}
float x[N];//存放结果的数组
for(i = N - 1;i>=0;i--){
x[i]=a[i][N];
for(j = i+1;j<N;j++){
x[i] -= a[i][j]*x[j];
}
x[i]/=a[i][i];
}
printf("结果为:\n");
for(i=0;i<N;i++){
printf("x[%d] = %f\n",i,x[i]);
}
}
大佬,我抄的你的代码为什么寄掉了,是哪里没改对吗? |
|