This homework has two parts. You should write both methods in the same class, ca
ID: 3538811 • Letter: T
Question
This homework has two parts. You should write both methods in the same class, called Recursion1.java. Your homework will not be accepted late (after Sunday, July 7th). You will recieve no credit for an program that does not compile.
The first method you write will have the following header:
This method computes the factorial of a number using the recursive definition of factorial we developed in class.
The second method will have the following header:
This method finds the average of the first uptowhere values in the array. For example, if the array is [2 4 6 8 10 12], and uptowhere is 4, the method will return 5 (the average of the first four elements in the array. Use recursion to implement a dynamic algorithm to solve this problem.
The output of the program is:
A second program:
int x = Recursion1.factorial(4);
}
Explanation / Answer
I have written the recurrsive function what you expected and I have tested with your DriverOne and DriverTwo classes, It got worked perfectly. Please test with your inputs.
public class Recursion1 {
public static float average(int arr[], int length){
if(length == 1){
return arr[0];
}else if(arr.length == length){
return (average(arr, length-1) + arr[length-1])/arr.length;
}else{
return (average(arr, length-1) + arr[length-1]);
}
}
public static int factorial(int number){
if(number == 0){
return 1;
}else{
return number*factorial(number-1);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.