forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx4.java
More file actions
34 lines (30 loc) · 890 Bytes
/
Copy pathEx4.java
File metadata and controls
34 lines (30 loc) · 890 Bytes
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
import java.util.Arrays;
import java.util.Random;
public class Ex4 {
public static void main(String[] args) {
int[] randomArray = createRandomArray(10);
System.out.print("The max value is located at index: ");
System.out.println(indexOfMax(randomArray));
}
public static int[] createRandomArray(int length) {
Random random = new Random();
int[] randomArray = new int[length];
for (int i = 0; i < randomArray.length; i++) {
randomArray[i] = random.nextInt(100);
}
System.out.print("Random array created: ");
System.out.println(Arrays.toString(randomArray));
return randomArray;
}
public static int indexOfMax(int[] numbers) {
int max = 0;
int maxIndex = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
maxIndex = i;
}
}
return maxIndex;
}
}