Created
November 13, 2019 18:21
-
-
Save wagfim/80c1d36a189aa2db14e1fdd23cc78ccf to your computer and use it in GitHub Desktop.
Gauss Jordan escalonamento matriz redução java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GFG { | |
static int M = 10; //?? | |
// exibe a matriz | |
static void exibeMatriz(float a[][], int n) { | |
String vermelhoCiano = "\033[31;46m"; | |
String reset = "\033[0m"; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j <= n; j++) { | |
if (i == j) { | |
System.out.print(vermelhoCiano + a[i][j] + reset +" | "); | |
} else { | |
System.out.print(a[i][j] + " | "); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
//funcao para reduzir a matriz | |
// escalonação por linha | |
static int executaOperacoes(float a[][], int n) { | |
int i, j, k = 0, c, flag = 0, m = 0; | |
float pro = 0; | |
// operações elementares | |
for (i = 0; i < n; i++) { | |
if (a[i][i] == 0) { | |
c = 1; | |
while (a[i + c][i] == 0 && (i + c) < n) { | |
c++; | |
} | |
if ((i + c) == n) { | |
flag = 1; | |
break; | |
} | |
for (j = i, k = 0; k <= n; k++) { | |
float temp = a[j][k]; | |
a[j][k] = a[j + c][k]; | |
a[j + c][k] = temp; | |
} | |
} | |
for (j = 0; j < n; j++) { | |
// exclui diagonal principal | |
if (i != j) { | |
// converte matriz para linha reduzida | |
// escalonamento (diagonal ) | |
float p = a[j][i] / a[i][i]; | |
for (k = 0; k <= n; k++) { | |
a[j][k] = a[j][k] - (a[i][k]) * p; | |
} | |
} | |
} | |
} | |
return flag; | |
} | |
//funcao que exibe o resultado desejado se soluções unicas existem | |
//caso contrario exibe sem solução ou infinitas soluções | |
//a depender da entrada | |
static void PrintResult(float a[][], int n, int flag) { | |
System.out.print("Resultado: "); | |
if (flag == 2) { | |
System.out.println("Infinitas soluções"); | |
} else if (flag == 3) { | |
System.out.println("Não existe solução"); | |
} | |
//exibe a solução exibindo as constante por suas respectivas diagonais | |
//carece de correção para 1 | |
else { | |
char letra = 'x'; | |
for (int i = 0; i < n; i++) { | |
System.out.print(letra++ + ": "+ a[i][n] / a[i][i] + " "); | |
} | |
} | |
} | |
//checa infinatas soluções ou sem solução | |
static int checarConsistencia(float a[][], int n, int flag) { | |
int i, j; | |
float soma; | |
// flag == 2 solução infinita | |
// flag == 3 sem solução | |
flag = 3; | |
for (i = 0; i < n; i++) { | |
soma = 0; | |
for (j = 0; j < n; j++) { | |
soma = soma + a[i][j]; | |
} | |
if (soma == a[i][j]) { | |
flag = 2; | |
} | |
} | |
return flag; | |
} | |
// funcao principal | |
public static void main(String[] args) { | |
Scanner leitor = new Scanner(System.in); | |
int n = 3, flag = 0; | |
/* | |
float a[][] = {{0, 2, 1, 4}, | |
{1, 1, 2, 6}, | |
{2, 1, 1, 7}}; | |
*/ | |
//0 2 1 4 1 1 2 6 2 1 1 7 | |
System.out.println("Qual a ordem da matriz"); | |
n = Integer.parseInt(leitor.next()); | |
float a[][] = new float[n][4]; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < 4; j++) { | |
System.out.println("Informe o binomio " + (j+1) + " da linha " + (i + 1)); | |
a[i][j] = leitor.nextFloat(); | |
} | |
} | |
// ordem da matriz | |
// executa operacao | |
flag = executaOperacoes(a, n); | |
if (flag == 1) { | |
flag = checarConsistencia(a, n, flag); | |
} | |
// exibe a matriz final | |
System.out.println("\nMatriz final: "); | |
exibeMatriz(a, n); | |
System.out.println(""); | |
// soluções, caso se aplique | |
PrintResult(a, n, flag); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment