-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultyArray.java
More file actions
85 lines (58 loc) · 1.68 KB
/
MultyArray.java
File metadata and controls
85 lines (58 loc) · 1.68 KB
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
80
81
82
83
84
85
/*
i valori che superano la soglia dopo la f cercafigura diventano 1.0 in caso contrario 0.0
*/
import java.text.DecimalFormat;
public class MultyArray {
public static void popolaArray(double figura[][]) {
for (int i = 0; i < figura.length; i++)
{
for (int j = 0; j < figura.length; j++)
{
figura[i][j] = ((double)Math.random() * 8) +1;
}
}
}
public static double[][] cercaFigura(double[][] figura, double soglia) {
double[][] array = figura;
for (int i =0; i < figura.length; i++) {
for (int j = 0; j < figura.length; j++) {
if (figura[i][j] < soglia ) {
figura[i][j] = 0.0;
array[i][j] = figura[i][j];
} else {
figura[i][j] = 1.0;
array[i][j] = figura[i][j];
}
}
}
return array;
}
private static DecimalFormat df2 = new DecimalFormat("#.##");
public static void stampaArray(double[][] figura) {
for (int i=0; i<figura.length; i++) {
for(int j=0; j<figura.length; j++) {
System.out.print(df2.format(figura[i][j]) +" " );
}
System.out.println();
}
}
public static double calcolaSoglia(double figura[][]) {
double soglia = 0.0;
for (int i =0; i < figura.length; i++) {
for (int j = 0; j < figura.length; j++) {
soglia += figura[i][j];
}
}
return soglia / Math.pow(figura.length, 2) ;
}
public static void main(String[] args) {
double[][] figura = new double [4][4];
popolaArray(figura);
System.out.println("Primo array");
System.out.println("");
stampaArray(figura);
System.out.println("");
System.out.println("Secondo array");
stampaArray(cercaFigura(figura,calcolaSoglia(figura)));
}
}