forked from suman-shah/c-program-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GaussianElimination.c
57 lines (55 loc) · 1.3 KB
/
GaussianElimination.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
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
// Array B contains the coefficients of the n 'n degree' equations long with constant term.
// Array A is used to implement the Gaussian Elimination method to solve linear equations.
// Array Y contains final generated output ans
double B[n + 1][n];
double A[n][n + 1];
for (int i = 0; i < n + 1; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%lf", &B[i][j]);
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
A[i][j] = B[i][j];
}
}
for (int i = 0; i < n; i++)
{
A[i][n] = B[n][i];
}
for (int b = 0; b < n - 1; b++)
{
for (int i = b + 1; i < n; i++)
{
double z = (A[i][b] / A[b][b]);
for (int j = 0; j < n + 1; j++)
{
A[i][j] = A[i][j] - (z * A[b][j]);
}
}
}
double Y[n];
Y[n - 1] = (A[n - 1][n] / A[n - 1][n - 1]);
for (int i = n - 2; i >= 0; i--)
{
double sum = 0;
for (int j = i + 1; j < n; j++)
{
sum += A[i][j] * Y[j];
}
Y[i] = (A[i][n] - sum) / A[i][i];
}
for (int i = 0; i < n; i++)
{
printf("%.7lf\n", Y[i]);
}
}