Question
Please Provide The Code In Java! And Please Provide The Trace with array = [1, -2, 3] and idx = 0. Using a picture Preferably
Please Provide The Code In Java! And Please Provide The Trace with array = [1, -2, 3] and idx = 0. Using a picture Preferably please explain how you did the trace as well!
R03: Sum positives only Write a recursive method that takes an array and returns the sum of only the positive integers. Example Input Expected Output [1, 2, 3] 6 [-3, 4, -6, -10, 8] 12 [-3, -2, -1] 0 [] 0 For this one, we need to consider the arguments to the method. First, we will need the array but for each element we need an index to retrieve from the array. Lets look at a purely iterative solution to see what this means. for(int idx = 0; idx < array.length; idx += 1) { if(array[idx] > 0) { sum += array[idx]; } } This should already give you an idea of what the base case should be. In the above, we stop our iteration when our index variable, idx, is equal to or greater than the length of the array. Thats because once weve reached the end of the array, weve done the checks for each value. Our base case will become If our index variable is equal to or greater than the arrays length, we should return 0. Also, if the index is less than 0 we should return 0. Why return 0? Well, we need to return something according to the method header and 0 wont affect the final result. Next is the recursive call. Actually, there will be 2 recursive calls. Look at the above loop again. There is an implicit assumption that if the number is less than or equal to 0 that we do nothing but we keep going. If we just stick to one recursive call we will not get every value. We need to actually perform a check and do two different calls. Lets think about this. If the number at array[idx] is positive then we need to add it to the final sum. If the number at array[idx] is not a positive value then we need to skip it and move on to the next number. This leaves us with the following
If(array[idx] > 0) { Add the value at array[idx] to the sum of the rest of the positive integers, if there are any, in the array. } else { Skip this value and move on to the next number. } Youll find that the recursive calls are the exact same but we have to do an extra calculation for one of the lines. To Add the value at array[idx] to sum of the rest of the positive integer we just do that, array[idx] + recursive call. To skip the number and move on we simply do not do the addition and just make the recursive call. This gives us all the information needed to complete the pseudocode. Int sumPositives(int array[], int idx) { If(array[idx] is less than 0 or greater than array length) { Return 0; } If(array[idx] > 0) { Return array[idx] + sumPositives(rest of array); } else { Return sumPositives(rest of array); } } Perform a trace with array = [1, -2, 3] and idx = 0.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started