Question
Run UtilRunner to test your code as you implement the methods below. Disregard the output for methods you have not implemented yet. Methods iterativeArraySum(int[] array)
Run UtilRunner to test your code as you implement the methods below. Disregard the output for methods you have not implemented yet.
Methods iterativeArraySum(int[] array) and recursiveArraySum(int[] array)
These methods add all of the values in an integer array. They each accept an array of int values as a parameter and return a long value.
Complete iterativeArraySum(). In this method, use a loop to add all of the values in the array and return the sum.
In order to use recursion, the recursiveArraySum() method will need to send itself more than just the array alone. The method will pass the array to itself, plus an index of the array to work on. To do this, the starter method, recursiveArraySum(), will accept an array just like the iterativeArraySum() method does. Internally, however, recursiveArraySum() will pass the array and a starting index value (0) to another method, recursiveArraySumHelper(), which will do the actual recursive calculations. The recursiveArraySum() starter method is provided for you.
In recursiveArraySumHelper(), use recursion to calculate and return the sum of all the values in the array. If the current index is greater than or equal to the array length, return 0. Otherwise, return the value of the array at the current index, plus the result of recursiveArraySumHelper() at index + 1. Do not use any loop statements in recursiveArraySumHelper().
Methods iterativeCountOnlyEvens(int[] array) and recursiveCountOnlyEvens(int[] array)
These methods accept an array as an argument and count the number, not the sum, of even numbers in the array. For example, an array with the values 4, 5, 6, 7, and 8, has three even values.
Complete iterativeCountOnlyEvens(). In this method, use a loop to count the number of even numbers in the array and return that value.
The recursiveCountOnlyEvens() starter method is provided for you. This method passes the array and a starting index value (0) to recursiveCountOnlyEvensHelper(), which will do the actual recursive calculations.
In recursiveCountOnlyEvensHelper(), use recursion to count and return the total number of even numbers in the array. Do not use any loop statements in recursiveCountOnlyEvensHelper(). If the current index is greater than or equal to the array length, return 0. Otherwise, return 1 if the current number is even, plus the result of recursiveCountOnlyEvensHelper() at index + 1.
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