-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgauss_jordan.c
84 lines (72 loc) · 1.22 KB
/
gauss_jordan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <math.h>
#define MAX 10
int main()
{
int i, j, k, n, pivrow;
float a[MAX][MAX], b[MAX], large, temp, factor, pivot;
printf("Input the number of variables: ");
scanf("%d", &n);
printf("Input coefficients a(i,j) row-wise (one row at a line): ");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%f", &a[i][j]);
}
}
printf("Enter vector b:");
for (i = 1; i <= n; i++)
{
scanf("%f", &b[i]);
}
for (i = 1; i <= n; i++)
{
pivrow = i;
large = a[i][i];
for (k = i + 1; k <= n; k++)
{
if (fabs(a[k][i] > large))
{
large = a[k][i];
pivrow = k;
}
}
if (pivrow != i)
{
for (j = i; j <= n; j++)
{
temp = a[pivrow][j];
a[pivrow][j] = a[i][j];
a[i][j] = temp;
}
temp = b[pivrow];
b[pivrow] = b[i];
b[i] = temp;
}
pivot = a[i][i];
for (j = 1; j <= n; j++)
{
a[i][j] = a[i][j] / pivot;
}
b[i] = b[i] / pivot;
for (j = 1; j <= n; j++)
{
if (j != i)
{
factor = a[j][i];
for (k = i; k <= n; k++)
{
a[j][k] = a[j][k] - factor * a[i][k];
}
b[j] = b[j] - factor * b[i];
}
}
}
printf("\nSolution vector x:\n");
for (i = 1; i <= n; i++)
{
printf("%f\t", b[i]);
}
return 0;
}