-
Notifications
You must be signed in to change notification settings - Fork 1
/
5-Gauss.pas
118 lines (101 loc) · 1.96 KB
/
5-Gauss.pas
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
PROGRAM gauss;
USES crt;
TYPE
Matr = ARRAY [1..20, 1..20] OF REAL;
Vect = ARRAY [1..20] OF REAL;
PROCEDURE MainEl(VAR a: Matr; VAR f: Vect; t, m: WORD);
VAR
i,j,k,s: WORD;
u: Vect;
p: REAL;
BEGIN
FOR j := t TO m DO
BEGIN
p := a[j,j];
FOR i := j TO m DO
BEGIN
IF Abs(a[i,j]) >= Abs(p) THEN
BEGIN
p := a[i,j];
s := i;
END;
END;
FOR k := 1 TO m DO
BEGIN
u[k] := a[s,k];
a[s,k] := a[j,k];
a[j,k] := u[k];
END;
p := f[s];
f[s] := f[j];
f[j] := p;
IF Abs(a[j,j]) = 0 THEN
BEGIN
WriteLn;
WriteLn('Matrix is degenerate!');
ReadKey;
Exit;
END;
END;
END;
{Solution}
VAR
a: Matr;
f, x: Vect;
i,j,n,k: WORD;
t: REAL;
BEGIN
ClrScr;
Write('Input dimension = '); ReadLn(n);
IF n < 2 THEN
WriteLn('Need n >= 2!');
FOR i := 1 TO n DO
BEGIN
FOR j := 1 TO n DO
BEGIN
Write('a(', i, ', ', j, ') = '); ReadLn(a[i,j]);
END;
Write('f(', i, ') = '); ReadLn(f[i]);
END;
FOR i := 1 TO n DO
BEGIN
MainEl(a,f,i,n);
t := a[i,i];
f[i] := f[i] / t;
FOR j := 1 TO n DO
BEGIN
a[i,j] := a[i,j] / t;
END;
FOR k := (i+1) TO n DO
BEGIN
t := a[k,i];
f[k] := f[k] - f[i] * t;
FOR j := i TO n DO
BEGIN
a[k,j] := a[k,j] - a[i,j] * t;
END;
END;
END;
WriteLn;
FOR i := 1 TO n DO
BEGIN
FOR j := 1 TO n DO
BEGIN
Write(' ', a[i,j]:3:1);
END;
WriteLn(' || ', f[i]:3:1);
END;
WriteLn;
FOR i := n DOWNTO 1 DO
BEGIN
t := 0;
FOR j := n DOWNTO 1 DO
BEGIN
t := t + a[i,j] * x[j];
END;
x[i] := f[i] - t;
END;
FOR i := 1 TO n DO
WriteLn(x[i]:3:2);
ReadKey;
END.