-
-
Save folivetti/0fa372f55bf1c370a31c1ddb26b443b6 to your computer and use it in GitHub Desktop.
This file contains 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 Main { | |
public static void imprimeMatriz(int [][] A) { | |
for(int i=0; i<A.length; i++) { | |
for(int j=0; j<A[0].length; j++) { | |
System.out.print(A[i][j]+" "); | |
} | |
System.out.println(); | |
} | |
} | |
public static int [][] somaMatrizes(int [][] A, int [][] B) { | |
int [][] C = new int [A.length][A[0].length]; | |
if (A.length != B.length || A[0].length != B[0].length) return C; | |
for(int i=0; i<A.length; i++) { | |
for(int j=0; j<A[0].length; j++) { | |
C[i][j] = A[i][j] + B[i][j]; | |
} | |
} | |
return C; | |
} | |
public static int [][] transposta(int [][] A) { | |
int m = A.length; | |
int n = A[0].length; | |
int [][] At = new int [n][m]; | |
for (int i=0;i<n;i++) { | |
for (int j=0; j<m; j++) { | |
At[i][j] = A[j][i]; | |
} | |
} | |
return At; | |
} | |
public static boolean iguais(int [][] A, int [][] B) { | |
if (A.length!=B.length || A[0].length!=B[0].length) return false; | |
for (int i=0; i<A.length; i++) { | |
for (int j=0; j<A[0].length; j++) { | |
if(A[i][j] != B[i][j]) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
public static int somaDiag(int [][] A) { | |
if (A.length != A[0].length) return 0; | |
int soma = 0; | |
for (int i=0;i<A.length; i++) { | |
soma += A[i][i]; | |
} | |
return soma; | |
} | |
public static boolean simetrica(int [][] A) { | |
if (A.length != A[0].length) return false; | |
for (int i=0;i<A.length; i++) { | |
for (int j=0; j<A[0].length; j++) { | |
if (A[i][j] != A[j][i]) return false; | |
} | |
} | |
return true; | |
} | |
public static boolean identidade(int [][] A) { | |
if (A.length != A[0].length) return false; | |
for (int i=0; i<A.length; i++) { | |
for (int j=0; j<A[0].length; j++) { | |
if (i==j && A[i][j]!=1) return false; | |
if (i!=j && A[i][j]!=0) return false; | |
} | |
} | |
return true; | |
} | |
public static int somaDiagSec(int [][] A) { | |
if (A.length != A[0].length) return 0; | |
int soma = 0; | |
for (int i=0;i<A.length; i++) { | |
soma += A[i][A.length-1-i]; | |
} | |
return soma; | |
} | |
public static int somaLinha(int [][] A, int i) { | |
int soma = 0; | |
for (int j=0;j<A[0].length; j++) { | |
soma += A[i][j]; | |
} | |
return soma; | |
} | |
public static int somaColuna(int [][] A, int j) { | |
int soma = 0; | |
for (int i=0;i<A.length; i++) { | |
soma += A[i][j]; | |
} | |
return soma; | |
} | |
public static int [][] trocaDiags(int [][] A) { | |
if (A.length != A[0].length) return A; | |
for (int i=0;i<A.length; i++) { | |
int temp = A[i][i]; | |
A[i][i] = A[i][A.length-1-i]; | |
A[i][A.length-1-i] = temp; | |
} | |
return A; | |
} | |
public static int [][] multMatriz(int [][] A, int [][] B) { | |
int m = A.length; | |
int n = B[0].length; | |
int k = A[0].length; | |
int [][] C = new int [m][n]; | |
if (k != B.length) return C; // não dá para multiplicar | |
for (int i = 0; i<m; i++) { | |
for (int j = 0; j<n; j++) { | |
C[i][j] = 0; | |
for (int l = 0; l<k; l++) { | |
C[i][j] += A[i][l]*B[l][j]; | |
} | |
} | |
} | |
return C; | |
} | |
public static boolean valido(int [][] A, int i, int j) { | |
int m = A.length; | |
int n = A[0].length; | |
if ((i >= 0) && (i < m) && (j >= 0) && (j < n)) return true; | |
return false; | |
} | |
public static int somaVizinhos(int [][] A, int i, int j) { | |
int soma = 0; | |
for (int k=-1; k<=1; k++) { | |
for (int l=-1; l<=1; l++) { | |
if ( !(k==0 && l==0) && valido(A, i+k, j+l) ){ | |
soma += A[i+k][j+l]; | |
} | |
} | |
} | |
return soma; | |
} | |
public static void main(String[] args) { | |
int [][] A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; | |
int [][] S = {{1, 2, 3}, {2, 5, 6}, {3, 6, 9}}; | |
int [][] I = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; | |
imprimeMatriz(A); | |
System.out.println(); | |
imprimeMatriz(somaMatrizes(A,A)); | |
System.out.println(); | |
imprimeMatriz(transposta(A)); | |
System.out.println(); | |
System.out.println(iguais(A,A)); | |
System.out.println(somaDiag(A)); | |
System.out.println(simetrica(A)); | |
System.out.println(simetrica(S)); | |
System.out.println(identidade(A)); | |
System.out.println(identidade(I)); | |
System.out.println(somaDiagSec(A)); | |
System.out.println(somaLinha(A, 1)); | |
System.out.println(somaColuna(A, 2)); | |
imprimeMatriz(trocaDiags(A)); | |
System.out.println(); | |
imprimeMatriz(multMatriz(A, S)); | |
imprimeMatriz(A); // reparem que a matriz A foi alterada | |
System.out.println(somaVizinhos(A, 2, 2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment