-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUtil.java
More file actions
49 lines (45 loc) · 1.28 KB
/
ArrayUtil.java
File metadata and controls
49 lines (45 loc) · 1.28 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
package exam01;
import java.util.Arrays;
public class ArrayUtil {
//counts how many elements are strictly above the average of each array,
//and returns these counts in an array
public static int[] countAboveAverages(int[]...arrays) {
// Create array to hold averages. What size is it?
double[] avgs = new double[arrays.length];
// Iterate over each array in arrays - store averages in avgs
for (int i=0; i<arrays.length; i++) {
double avg = 0;
if (arrays[i] != null && arrays[i].length != 0) {
for (int j=0; j<arrays[i].length; j++) {
avg += arrays[i][j];
}
}
avg = avg/arrays[i].length;
avgs[i] = avg;
}
if (arrays == null) {
throw new IllegalArgumentException("arrays was null.");
}
if (arrays.length == 0) {
return new int[0];
}
int[] aboveAvgCounts = new int[arrays.length];
// Iterate over arrays, and each array
for (int i=0; i< arrays.length; i++) {
for (int j=0; j<arrays[i].length; j++) {
System.out.println(arrays[i][j]);
}
}
for (int i=0; i<arrays.length; i++) {
aboveAvgCounts[i] = 0;
if (arrays[i] != null && arrays[i].length > 0) {
for (int j=0; j<arrays[i].length; j++) {
if (arrays[i][j] > avgs[i]) {
aboveAvgCounts[i] += 1;
}
}
}
}
return aboveAvgCounts;
}
}