Last active
November 17, 2017 20:47
-
-
Save diogocapela/4cbd792daad2e422ee3e465e2b846b32 to your computer and use it in GitHub Desktop.
APROG - PL08/09 Estruturas de Dados Indexadas
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
O programa faz print ao maior elemento de cada uma das linhas da matriz. |
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
public static void mostrarMatriz(int[][] matriz) { | |
for(int i = 0; i < matriz.length; i++) { | |
for(int k = 0; k < matriz[i].length; k++) { | |
System.out.print(matriz[i][k] + " "); | |
} | |
System.out.println(""); | |
} | |
} |
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
public static void mostrarMedias(int[][] matriz) { | |
int quantidadeTotal = 0; | |
int somatorioTotal = 0; | |
double mediaGlobal; | |
int[] quantidadeColunas = new int[matriz[0].length]; | |
int[] somatorioColunas = new int[matriz[0].length]; | |
double[] mediaColunas = new double[matriz[0].length]; | |
for(int i = 0; i < matriz.length; i++) { | |
for(int k = 0; k < matriz[i].length; k++) { | |
quantidadeTotal++; | |
somatorioTotal = somatorioTotal + matriz[i][k]; | |
quantidadeColunas[k]++; | |
somatorioColunas[k] = somatorioColunas[k] + matriz[i][k]; | |
} | |
} | |
mediaGlobal = (double) somatorioTotal / quantidadeTotal; | |
System.out.println("Média global: " + mediaGlobal); | |
for(int i = 0; i < mediaColunas.length; i++) { | |
mediaColunas[i] = (double) somatorioColunas[i] / quantidadeColunas[i]; | |
System.out.println("Média da coluna " + i + ": " + mediaColunas[i]); | |
} | |
} |
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
public static void mostrarMatrizTransposta(int[][] matriz) { | |
int[][] matrizTransposta = new int[matriz[0].length][matriz.length]; | |
for(int i = 0; i < matriz.length; i++) { | |
for(int k = 0; k < matriz[i].length; k++) { | |
matrizTransposta[k][i] = matriz[i][k]; | |
} | |
} | |
mostrarMatriz(matrizTransposta); | |
} |
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
// Está errado, é necessário completar | |
public class Main { | |
public static void main(String[] args) { | |
int m[][] = { | |
{1, 4, 2, 1}, | |
{9, 7, 2, 2}, | |
{1, 7, 3, 5}, | |
{2, 5, 0, 3}, | |
{4, 7, 2, 1} | |
}; | |
boolean resposta = verificarSeMatrizEQuadradoMagico(m); | |
System.out.println("R: " + resposta); | |
} | |
public static boolean verificarSeMatrizEQuadradoMagico(int[][] matriz) { | |
boolean verificacao = true; | |
int somaPrimeira = 0; | |
if(matriz.length != matriz[0].length) { | |
verificacao = false; | |
} | |
for(int i = 0; i < matriz.length; i++) { | |
int somaLinhas = 0; | |
int somaColunas = 0; | |
int somaDiagonal1st = 0; | |
int somaDiagonal2nd = 0; | |
somaPrimeira = somaPrimeira + matriz[0][i]; | |
for(int k = 0; k < matriz[i].length; k++) { | |
somaLinhas = somaLinhas + matriz[i][k]; | |
somaColunas = somaColunas + matriz[k][i]; | |
somaDiagonal1st = somaDiagonal1st + matriz[i][i]; | |
somaDiagonal2nd = somaDiagonal2nd + matriz[matriz.length - i][i]; | |
} | |
if(somaLinhas != somaPrimeira || somaColunas != somaPrimeira || somaDiagonal1st != somaPrimeira || somaDiagonal2nd != somaPrimeira) { | |
verificacao = false; | |
} | |
} | |
return verificacao; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment