-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gaussian_elimination.c
60 lines (53 loc) · 916 Bytes
/
Gaussian_elimination.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
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
printf("Enter number of unknowns: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%f", &a[i][j]);
}
}
for(i=1;i<=n-1;i++)
{
// if(a[i][i] == 0.0)
// {
// printf("Mathematical Error!");
// exit(0);
// }
for(j=i+1;j<=n;j++)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
x[n] = a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
x[i] = a[i][n+1];
for(j=i+1;j<=n;j++)
{
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
printf("\nSolution:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = %0.3f\n",i, x[i]);
}
getch();
return(0);
}