简体   繁体   中英

Take Average of an Array using Recursion

I'm trying to create a program that will take a user input, input that data into an dynamic array, and then recursively finds the average. The first part of my code works. This allows the newly created array to be passed to the method.

 public static void main(String args[])
{
    
    int i = 0;
    int sum = 0;
    double runningTotal = 0;
    
    int classSize;
    Scanner keyboard = new Scanner(System.in);      
    System.out.println("Please enter the class size: ");
    classSize = keyboard.nextInt();
    int newClassSize[] = new int[classSize];
    
    for (i=0; i < newClassSize.length; i++)
    {
        System.out.println("Please enter the grade of the user at: " + (i + 1));
        newClassSize[i] = keyboard.nextInt();   
    }
    
    findAverage();
    
    
    for (i=0; i < newClassSize.length; i++){
                
        
        sum = sum + newClassSize[i];
    }
                    
    System.out.println(Arrays.toString(newClassSize));
            
    
keyboard.close();
    
}
}

This is where I'm getting confused and confusing myself however. How would I pass the newly created array to the findAverage() method? I would then need to also have that be saved to an accumulator and then devided. Is there a better way to do this? This is my current findAverage() method but I'm confusing myself on my implementation.

public double findAverage(int classAverage, int baseCase, double runningAverage)
{
    runningAverage = 0;
    int sum = 0;
    
    if (newClassSize.length - 1 > baseCase)
        runningAverage = newClassSize.length;
        
    
    return findAverage();
    System.out.println("The class average is " + classAverage);
    
}
    

Hopefully I understood your question correctly but heres how to do it below. The basic idea is that when the index reaches the length of the array in the recursive function that's the base case. So all you have to do is add to the sum at each index point in the array, and just keep passing in the updated index and sum into the recursive function.

class Main {
    public static void main(String[] args) {
        int newClassSize[] = {1,2,3}; // User Input let say

        
        double average = findAverage(newClassSize);
        System.out.println(average);
    }
    
    
    public static  double findAverage(int[] arr){
        // Avoid division by zero error
        if (arr.length==0){
           return 0;
        }
        return findAverageHelper(arr,0,0);
    }
    
    
    public static  double findAverageHelper(int[] arr, int index,int sum){
        if (index==arr.length){ // Base Case 
            return (double) sum/arr.length;
        }
        // Increase index and add current value at index to sum
        return findAverageHelper(arr,index+1,sum+=arr[index]);
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM