Fun Gauss

 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
85
86
87
88
89
90
91
92
93
94
Program Slar;
uses crt;
type
matr = array [1..10, 1..10] of real;
vect = array [1..10] of real;
var
a: matr;
x: vect;
n, i, j: integer;
Procedure Exchange(i, k: integer);
var
temp: real;
m: integer;
begin
if i = k then exit;
for m := k to n + 1 do begin
temp := a[i, m];
a[i, m] := a[k, m];
a[k, m] := temp;
end;
end;
Function Gauss(n: integer; a: matr; var x: vect): boolean;
const e = 0.00000001;
var
i, j, k, q: integer;
sum, max: real;
imax: integer;
begin
for k := 1 to n do begin
imax := k;
max := abs(a[k, k]);
for q := k + 1 to n do begin
if abs(a[q, k]) > max then begin
max := abs(a[q, k]);
imax := q;
end;
end;
end;
if abs(max) < e then begin
Gauss := false;
exit
end else begin
Gauss := true;
Exchange(imax, k);
for i := k + 1 to n do begin
for j := n + 1 downto k do begin
a[i, j] := a[i, j] - a[i, k] / a[k, k] * a[k, j];
end;
end;
end;
for i := 1 to n do begin
for j := 1 to n + 1 do begin
write('a[', i, ',', j, '] = ', a[i, j]:2:2, ' ');
writeln;
end;
end;
{Зворотній хід}
x[n] := a[n, n + 1] / a[n, n];
for i := n - 1 downto 1 do begin
sum := 0;
for k := i + 1 to n do begin
sum := sum + x[k] * a[i, k];
a[i, n + 1] := a[i, n + 1] - sum;
x[i] := a[i, n + 1] / a[i, i];
end;
end;
end;
begin
clrscr;
{Читаємо кількість рівнянь}
read(n);
{Читаємо коефіцієнти рівнянь}
for i := 1 to n do begin
for j := 1 to n + 1 do begin
read(a[i, j]);
write('a[', i, ',', j, '] = ', a[i, j]:2:2, ' ');
end;
writeln;
end;
writeln;
{Викликаємо функцію Гауса}
if Gauss(n, a, x) then begin
writeln;
{Виводимо розв’язки на екран}
for i := 1 to n do begin
writeln('x[', i, '] = ', x[i]:2:2, ' ');
end;
end else begin
writeln('System do not have a solution');
end;
end.