列主元消去法代码的c语言代码
时间: 2023-05-21 19:03:55 浏览: 78
使用列主元消元法解方程组c语言代码.docx
以下是列主元消去法的C语言代码:
#include <stdio.h>
#define N 3
int main()
{
float a[N][N+1] = {{2, 1, -1, 8}, {-3, -1, 2, -11}, {-2, 1, 2, -3}};
int i, j, k, max;
float temp, x[N];
for (i = 0; i < N-1; i++) {
max = i;
for (j = i+1; j < N; j++) {
if (a[j][i] > a[max][i]) {
max = j;
}
}
for (k = i; k < N+1; k++) {
temp = a[i][k];
a[i][k] = a[max][k];
a[max][k] = temp;
}
for (j = i+1; j < N; j++) {
temp = a[j][i] / a[i][i];
for (k = i; k < N+1; k++) {
a[j][k] -= temp * a[i][k];
}
}
}
x[N-1] = a[N-1][N] / a[N-1][N-1];
for (i = N-2; i >= 0; i--) {
temp = a[i][N];
for (j = i+1; j < N; j++) {
temp -= a[i][j] * x[j];
}
x[i] = temp / a[i][i];
}
for (i = 0; i < N; i++) {
printf("x%d = %f\n", i+1, x[i]);
}
return 0;
}
阅读全文