-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgausselim.c
More file actions
61 lines (49 loc) · 1.22 KB
/
Copy pathgausselim.c
File metadata and controls
61 lines (49 loc) · 1.22 KB
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
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main(){
int n;
printf("please enter n of nxn matrix: ");
scanf("%d",&n);
float a[n][n+1],sum=0;
float x[n],c;
// input
for (int i = 0; i < n ; i++){
for (int j = 0; j < n + 1; j++){
printf("enter a[%d][%d] = ",i,j);
scanf("%f",&a[i][j]);
}
}
// triangularisation
for (int i = 0; i <= n - 2; i++)
{
for (int k = 1+i; k <= n-1; k++)
{
c = (a[k][i]/a[i][i]);
for (int j = 0; j <= n; j++)
{
// c = (a[k][i]/a[i][i])
a[k][j] = a[k][j] - c*a[i][j];
}
}
}
// testing printing
for (int i = 0; i < n ; i++){
for (int j = 0; j < n + 1; j++){
printf("%f \t",a[i][j]);
}
printf("\n");
}
x[n-1] = a[n-1][n]/a[n-1][n-1];
printf("%f \n",x[n-1]);
for (int i = n-2; i >= 0; i--)
{
sum = 0;
for (int j = i+1; j <= n-1; j++){
sum += a[i][j]*x[j] ;
}
x[i] = (a[i][n] - sum)/a[i][i] ;
printf("%f \n",x[i]);
}
return 0;
}