forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx1.java
More file actions
43 lines (38 loc) · 1.32 KB
/
Copy pathEx1.java
File metadata and controls
43 lines (38 loc) · 1.32 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
import java.util.Arrays;
import java.util.Random;
public class Ex1 {
public static void main(String[] args) {
double[] values = {1.0, 2.0, 3.0, 4.0, 5.0};
System.out.print("Original array: ");
System.out.println(Arrays.toString(values));
powArray(values, 2);
int[] scores = createScoresArray(100);
histogram(scores);
}
public static void powArray(double[] array, int power) {
double[] newArray = Arrays.copyOf(array, array.length);
for (int i = 1; i < array.length; i++) {
newArray[i] = Math.pow(array[i], power);
}
System.out.print("Original array to the power of " + power + ": ");
System.out.println(Arrays.toString(newArray));
}
public static int[] createScoresArray(int size) {
Random randomScore = new Random();
int[] randomArray = new int[size];
for (int i = 0; i < randomArray.length; i++) {
randomArray[i] = randomScore.nextInt(100);
}
System.out.print(size + " random scores: ");
System.out.println(Arrays.toString(randomArray));
return randomArray;
}
public static void histogram(int[] scores) {
System.out.print("Distribution of scores: ");
int[] scoreDistribution = new int[scores.length];
for (int score : scores) {
scoreDistribution[score]++;
}
System.out.println(Arrays.toString(scoreDistribution));
}
}