-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.h
executable file
·79 lines (73 loc) · 1.64 KB
/
matrix.h
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
float matrix[mtx][mtx+1],solucao[mtx], zero;
int dim;
void troca_linha(int l1, int l2);
void imp_matrix();
void le_matrix();
void back_subst();
void zero_maquina();
int testa_matrix();
void troca_linha(int l1, int l2){
float aux[mtx+1];
for (int i=0;i<mtx+1;i++) {
aux[i]=matrix[l1][i];
matrix[l1][i]=matrix[l2][i];
matrix[l2][i]=aux[i];
}
}
void imp_matrix() {
printf("\n");
for (int i=0;i<dim;i++){
for(int j=0;j<=dim;j++) {
printf("%10.4f",matrix[i][j]);
}
printf("\n");
}
}
void le_matrix() {
printf("\nEntre com a dimensao do sistema:");
scanf("%i",&dim);
for(int i=0;i<dim;i++){
for(int j=0;j<=dim;j++){
if (j==dim) { printf("Entre com o termo independente da %ia equacao: ",(i+1)); }
else { printf("Entre com a %ia variavel da %ia equacao: ",(j+1),(i+1)); }
scanf("%f",&matrix[i][j]);
}
}
printf("\nSistema fornecido:");
imp_matrix();
}
int testa_matrix(){
int ldz,cdz;
for (int i=0;i<dim;i++) {
ldz=0;
for (int j=0;j<=dim;j++) {
if (matrix[i][j] != 0) { ldz=1; }
}
if (ldz == 0) { break; }
}
for (int i=0;i<dim;i++) {
cdz=0;
for (int j=0;j<dim;j++) {
if (matrix[j][i] != 0) { cdz=1; }
}
if (cdz == 0) { break; }
}
return (cdz && ldz);
}
void back_subst() {
for(int i=dim-1;i>=0;i=i-1) {
solucao[i]=matrix[i][dim];
for (int j=i+1;j<dim;j++) {
solucao[i]=solucao[i]-(matrix[i][j]*solucao[j]);
}
solucao[i]=solucao[i]/matrix[i][i];
}
}
void zero_maquina() {
float zero_da_maquina=1, teste;
do {
zero_da_maquina=zero_da_maquina/2;
teste=1+zero_da_maquina;
} while (teste > 1);
zero=zero_da_maquina;
}