Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class ArrayHomework {
- public static void main(String[] args) {
- int max = 20;
- double[] arr = new double[max];
- fillArray(arr);
- // Let's print the array for educational purposes.
- for (double d : arr) {
- System.out.println(d);
- }
- System.out.println("The index of the largest number in the array is: " + findIndexOfLargest(arr));
- System.out.println("The largest number in the array is " + findLargest(arr));
- }
- public static void fillArray(double[] myArray) {
- Random random = new Random();
- for (int i = 0; i < myArray.length; i++) {
- myArray[i] = random.nextDouble();
- }
- }
- public static int findIndexOfLargest(double[] myArray) {
- // double max = Arrays.stream(myArray).filter(i -> i >= 0).max().orElse(0);
- double max = 0;
- int location = 0;
- for (int i = 0; i < myArray.length; i++) {
- if (myArray[i] > max) {
- max = myArray[i];
- location = i;
- }
- }
- return location;
- }
- public static double findLargest(double[] myArray) {
- //int loc = findIndexOfLargest(myArray);
- //return myArray[loc];
- double max = 0;
- for (int i = 0; i < myArray.length; i++) {
- if (myArray[i] > max) {
- max = myArray[i];
- }
- }
- return max;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment