-
Notifications
You must be signed in to change notification settings - Fork 0
/
metodo_gauss.cpp
80 lines (69 loc) · 2.08 KB
/
metodo_gauss.cpp
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
#include <iostream>
using namespace std;
void GaussElimination(int n, float matriz[20][20], float x[10])
{
float conta;
int linha, coluna, k;
float somador = 0;
// Zeroing elements below the main diagonal
for(coluna = 1; coluna <= n; coluna++)
{
for(linha = 1; linha <= n; linha++)
{
if(linha > coluna)
{
conta = -(matriz[linha][coluna] / matriz[coluna][coluna]);
for(k = 1; k <= (n+1); k++)
{
matriz[linha][k] = conta * matriz[coluna][k] + matriz[linha][k];
}
}
}
}
// Displaying the matrix after zeroing elements below the main diagonal
cout << "\nMatrix after zeroing elements below the main diagonal:\n";
for(linha = 1; linha <= n; linha++)
{
for(coluna = 1; coluna <= (n+1); coluna++)
{
cout << matriz[linha][coluna] << " ";
}
cout << endl;
}
// Calculating the solution
x[n] = matriz[n][n+1] / matriz[n][n];
for(linha = (n-1); linha >= 1; linha--)
{
somador = 0;
for(coluna = (linha+1); coluna <= n; coluna++)
{
somador = somador + matriz[linha][coluna] * x[coluna];
}
x[linha] = (matriz[linha][n+1] - somador) / (matriz[linha][linha]);
}
// Displaying the solution
cout << "\nSolution:\n";
for(linha = 1; linha <= n; linha++)
{
cout << "x[" << linha << "] = " << x[linha] << endl;
}
}
int main(int argc, char *argv[])
{
int n;
float matriz[20][20];
float x[10];
cout << "Enter the order of the matrix: ";
cin >> n;
cout << "Enter the elements of the augmented matrix:\n\n";
for(int linha = 1; linha <= n; linha++)
{
for(int coluna = 1; coluna <= (n+1); coluna++)
{
cout << "Matrix[" << linha << "][" << coluna << "] : ";
cin >> matriz[linha][coluna];
}
}
GaussElimination(n, matriz, x);
return 0;
}