binibiningtinamoran

ArrayHomework.java

Nov 2nd, 2020 (edited)
2,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class ArrayHomework {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         int max = 20;
  8.         double[] arr = new double[max];
  9.  
  10.         fillArray(arr);
  11.  
  12.         // Let's print the array for educational purposes.
  13.         for (double d : arr) {
  14.             System.out.println(d);
  15.         }
  16.  
  17.         System.out.println("The index of the largest number in the array is: " + findIndexOfLargest(arr));
  18.         System.out.println("The largest number in the array is " + findLargest(arr));
  19.  
  20.     }
  21.  
  22.  
  23.     public static void fillArray(double[] myArray) {
  24.         Random random = new Random();
  25.         for (int i = 0; i < myArray.length; i++) {
  26.             myArray[i] = random.nextDouble();
  27.         }
  28.     }
  29.  
  30.     public static int findIndexOfLargest(double[] myArray) {
  31.         // double max = Arrays.stream(myArray).filter(i -> i >= 0).max().orElse(0);
  32.  
  33.         double max = 0;
  34.         int location = 0;
  35.         for (int i = 0; i < myArray.length; i++) {
  36.             if (myArray[i] > max) {
  37.                 max = myArray[i];
  38.                 location = i;
  39.             }
  40.         }
  41.         return location;
  42.     }
  43.  
  44.     public static double findLargest(double[] myArray) {
  45.  
  46.         //int loc = findIndexOfLargest(myArray);
  47.         //return myArray[loc];
  48.  
  49.         double max = 0;
  50.         for (int i = 0; i < myArray.length; i++) {
  51.             if (myArray[i] > max) {
  52.                 max = myArray[i];
  53.             }
  54.         }
  55.         return max;
  56.  
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment