ADD SOURCE CODE TO THE FOLLOWING PROGRAM TO EXPLAIN CODE: import java.util.Array
ID: 3842982 • Letter: A
Question
ADD SOURCE CODE TO THE FOLLOWING PROGRAM TO EXPLAIN CODE:
import java.util.Arrays;
public class RecursiveAverage {
public static void main(String[] args) {
double arr[]= new double[10];
fillArray(arr, arr.length);
System.out.println("Array double values: ");
System.out.println(Arrays.toString(arr));
System.out.println("Average is "+getAverage(arr));
}
public static void fillArray(double arr[], int size){
if(size == 0){
return;
}
else{
arr[size-1] = Math.random();
fillArray(arr,size-1);
}
}
public static double getSum(double arr[], int size) {
if(size == 0){
return 0;
}
else{
return arr[size-1] + getSum(arr, size-1);
}
}
public static double getAverage(double arr[]) {
return getSum(arr, arr.length)/arr.length;
}
}
Explanation / Answer
The above program is first filling the array od double type and size 10 with random number in function fillArray(recursive) and using random function and then print the array followed by calculation average of elements by calling getAverage function( which internally calls getSum function to first calculate sum of elements).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.