Write a method public static doubled get Array Stats double

Write a method public static doubled [] get Array Stats (double[] a) that finds the minimum, average, and maximum elements in the array a. Your method should return an array containing a\'s minimum element in index 0, a\'s average value in index 1, and a\'s maximum element in index 2.

Solution

package org.students;

import java.util.Random;
import java.util.Scanner;

public class MinMaxAvgOfDoubleArray {

   public static void main(String[] args) {
       //Creating the double array of size 10
       double arr[]=new double[10];
       double minmaxavg[];
      
       //Creating Random class object
       Random rand=new Random();
      
       //this loop generates and populates 10 random numbers into an array  
       for(int i=0;i<arr.length;i++)
       {
       arr[i]= 10 + (90 - 10) * rand.nextDouble();
       }
      
       //Display the array elements
       System.out.println(\"The Elements in the array are :\");
       for(int i=0;i<arr.length;i++)
       {
       System.out.printf(\"%.2f \",arr[i]);
       }  
      
       //Calling the method by passing array as argument
minmaxavg=getArrayStats(arr);
  
//Displaying the minimum element of an array
System.out.printf(\"\ Minimum Element of an Array is : %.2f\",minmaxavg[0]);
  
//Displaying the maximum element of an array
System.out.printf(\"\ Maximum Element of an Array is : %.2f\",minmaxavg[1]);
  
//Displaying the Average of an array elements
System.out.printf(\"\ Average of all the emelents of an array is : %.2f\",minmaxavg[2]);
   }

   /* This method will find the minimum,maximum
   * and average values and populate them into an array
   * Params: double array
   * Return : double array
   */
   private static double[] getArrayStats(double[] arr) {
       //Declaring variables
       double minmaxavg[]=new double[3];
       double min,max,sum=0.0,avg=0.0;
      
       //Assigning the first element of an array to min and max variables
       min=arr[0];
       max=arr[0];
      
       //This for loop will find the minimum and maximum of an array
       for(int i=0;i<arr.length;i++)
       {
           //Calculating the sum of all the elements in the array
           sum+=arr[i];
          
           //Finding minimum element
           if(arr[i]<min)
           min=arr[i];
          
           //Finding maximum element
           if(arr[i]>max)
               max=arr[i];
       }
      
       //calculating the average of array elements
       avg=sum/arr.length;
      
       minmaxavg[0]=min;
       minmaxavg[1]=max;
       minmaxavg[2]=avg;
      
       return minmaxavg;
   }

}

____________________________________________

Output:

The Elements in the array are :
19.54 86.39 56.30 65.83 58.34 49.31 23.71 50.24 87.52 52.11
Minimum Element of an Array is : 19.54
Maximum Element of an Array is : 87.52
Average of all the emelents of an array is : 54.93

_____________________________________________

Output1:

The Elements in the array are :
39.27 56.22 68.92 38.35 38.38 23.99 89.34 43.49 51.58 28.33
Minimum Element of an Array is : 23.99
Maximum Element of an Array is : 89.34
Average of all the emelents of an array is : 47.79

_____________Thank You

 Write a method public static doubled [] get Array Stats (double[] a) that finds the minimum, average, and maximum elements in the array a. Your method should r
 Write a method public static doubled [] get Array Stats (double[] a) that finds the minimum, average, and maximum elements in the array a. Your method should r

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site