Completati esercizi matrici per 25-1-23

Completati esercizi Identità, Riempimento e Simmetria tra matrici + PDF
This commit is contained in:
Giacomo R. 2023-01-22 22:38:35 +01:00
parent da24fc76ae
commit c4e9db119c
6 changed files with 88 additions and 0 deletions

30
Matrici/Identita.java Normal file
View File

@ -0,0 +1,30 @@
public class Identita {
public static void main(String[] args) {
int[][] matrice =
{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 1}
};
System.out.println(cercaIdentita(matrice));
}
private static boolean cercaIdentita(int[][] matrice) {
for (int i = 0; i < matrice.length; i++) {
for (int j = 0; j < matrice[0].length; j++) {
if(matrice[i][i] != 1) {
return false;
}
if(i != j && matrice[i][j] != 0) {
return false;
}
}
}
return true;
}
}

BIN
Matrici/Identita.pdf Normal file

Binary file not shown.

26
Matrici/Riempimento.java Normal file
View File

@ -0,0 +1,26 @@
import java.util.Random;
public class Riempimento {
public static void main(String[] args) {
int[][] matrice = new int[5][5];
riempiRandom(matrice, 100);
for (int[] array : matrice) {
for(int num : array) {
System.out.printf("%2d ", num);
}
System.out.println();
}
}
public static void riempiRandom(int[][] matrice, int maxValue) {
Random generator = new Random();
for (int i = 0; i < matrice.length; i++) {
for (int j = 0; j < matrice[0].length; j++) {
matrice[i][j] = generator.nextInt(maxValue);
}
}
}
}

BIN
Matrici/Riempimento.pdf Normal file

Binary file not shown.

32
Matrici/Simmetria.java Normal file
View File

@ -0,0 +1,32 @@
public class Simmetria {
public static void main(String[] args) {
int[][] matrice =
{
{1, 2, 3, 4, 5},
{2, 1, 4, 5, 6},
{3, 4, 1, 6, 7},
{4, 5, 6, 1, 8},
{5, 6, 7, 8, 1}
};
for (int[] array : matrice) {
for(int num : array) {
System.out.printf("%2d ", num);
}
System.out.println();
}
boolean simmetrico = true;
for (int i = 0; i < matrice.length; i++) {
for (int j = 0; j < matrice[0].length; j++) {
if (matrice[i][j] != matrice[j][i]) simmetrico = false;
}
}
if(simmetrico) {
System.out.println("La matrice è simmetrica rispetto alla diagonale principale");
} else {
System.out.println("La matrice non è simmetrica");
}
}
}

BIN
Matrici/Simmetria.pdf Normal file

Binary file not shown.