include iostream include stdio include math include stdlib define defi

 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
#include<iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define N 3
#define N1 N+1
using namespace std;
float matrix[ N ][ N1 ] = {{2,-1,3,2},
{1,-2,1,3},
{3,-1,3,4}};
float epsilon = 0.001;
void ShowMatrix( void ) {
cout << "Input SLAU:" << endl;
int i, j;
for ( i = 0 ; i < N; i++ ) {
for ( j = 0; j < N; j++ )
printf( "%+3.3f*x%d", matrix[ i ][ j ], i + 1 );
printf( "=%3.3f\n", matrix[ i ][ N ] );
}
}
int main() {
float tmp, xx[ N1 ];
short int i, j, k;
ShowMatrix();
/*Метод Гаусса*/
/*Прямой ход*/
for ( i = 0; i < N; i++ ) {
tmp = matrix[ i ][ i ];
for ( j = N; j >= i; j-- )
matrix[ i ][ j ] /= tmp;
for ( j = i + 1; j < N; j++ ) {
tmp = matrix[ j ][ i ];
for ( k = N; k >= i; k-- )
matrix[ j ][ k ] -= tmp * matrix[ i ][ k ];
}
}
/*Обратный ход*/
xx[ N - 1 ] = matrix[ N - 1 ][ N ];
for ( i = N - 2; i >= 0; i-- ) {
xx[ i ] = matrix[ i ][ N ];
for ( j = i + 1; j < N; j++ )
xx[ i ] -= matrix[ i ][ j ] * xx[ j ];
}
/*Вывод решения системы*/
printf( "\nMethod Gaussa: \n" );
for ( i = 0; i < N; i++ )
printf( "x%d=%3.3f\n", i + 1, xx[ i ] );
return 0;
}