Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This method also takes in the parameter max and returns the fibonacci number cor

ID: 3529929 • Letter: T

Question

This method also takes in the parameter max and returns the fibonacci
number corresponding to the position of max. Fibonacci sequence consists
of numbers that are the sum of the previous two numbers. Starting from
the position 0, we get the sequence.
Position
0
1
2
3
4
5
6
7
8
Fib Num
0
1
1
2
3
5
8
13
21
So for position 6, it takes value of position 5 and 4 together to add them up.
So this method would return 8 by doing 3+5.
You will need to implement both versions of the method just like sumAll.
One version only takes one parameter and the other version takes an array
as an additional argument. Declare the methods as appropriate in the space
given in the file Lab21_7.java. Uncomment the invocation of fib() methods.


Output should be similar to lab 6 except there should be a pair of Called and
Return statements as sumAll sample output.


public class Lab21_7 {

public static void init(long[] arr) {
// Initialize all the entries of the array to 0
for (int i = 0; i < arr.length; i++) {
arr[i] = 0;
}
}

public static long sumAll(int max) {
// sumAll basic version
System.out.println("Called sumAll(" + max + ")");

long result;

if (max <= 1)
result = 1;
else
result = max + sumAll(max-1);
System.out.println("Return sumAll(" + max + ") = " + result);
return result;

}

public static long sumAll(long[] arr, int max) {
// Simple Recursion with array
System.out.println("Called sumAll(" + max + ")");

if (max <= 1)
arr[max] = 1;
else if (arr[max] == 0)
arr[max] = max + sumAll(arr, max - 1);

System.out.println("Return sumAll(" + max + ") = " + arr[max]);

return arr[max];
}

// Fill-in the appropriate methods to complete the lab

public static void main(String[] args) {

final int MAXSIZE = 100;
long[] arr = new long[MAXSIZE];

System.out.println("sumAll output for 5 is " + sumAll(5));
System.out.println("sumAll output for 10 is " + sumAll(10));
System.out.println("sumAll output for 20 is " + sumAll(20));
System.out.println("sumAll output for 15 is " + sumAll(15));
System.out.println();

init(arr);
System.out.println("sumAll output for 5 is " + sumAll(arr, 5));
System.out.println("sumAll output for 10 is " + sumAll(arr, 10));
System.out.println("sumAll output for 20 is " + sumAll(arr, 20));
System.out.println("sumAll output for 15 is " + sumAll(arr, 15));
System.out.println();

/*
System.out.println("squareSum output for 5 is " + squareSum(5));
System.out.println("squareSum output for 10 is " + squareSum(10));
System.out.println("squareSum output for 20 is " + squareSum(20));
System.out.println("squareSum output for 15 is " + squareSum(15));
System.out.println();

init(arr);
System.out.println("squareSum output for 5 is " + squareSum(arr, 5));
System.out.println("squareSum output for 10 is " + squareSum(arr, 10));
System.out.println("squareSum output for 20 is " + squareSum(arr, 20));
System.out.println("squareSum output for 15 is " + squareSum(arr, 15));
System.out.println();

System.out.println("Fibonacci Number 5 is " + fib(5));
System.out.println("Fibonacci Number 10 is " + fib(10));
System.out.println("Fibonacci Number 20 is " + fib(20));
System.out.println("Fibonacci Number 15 is " + fib(15));
System.out.println();

init(arr);
System.out.println("Fibonacci Number 5 is " + fib(arr, 5));
System.out.println("Fibonacci Number 10 is " + fib(arr, 10));
System.out.println("Fibonacci Number 20 is " + fib(arr, 20));
System.out.println("Fibonacci Number 15 is " + fib(arr, 15));
System.out.println();

System.out.println("Factorial of 5 is " + factorial(5));
System.out.println("Factorial of 10 is " + factorial(10));
System.out.println("Factorial of 20 is " + factorial(20));
System.out.println("Factorial of 15 is " + factorial(15));
System.out.println();

init(arr);
System.out.println("Factorial of 5 is " + factorial(arr, 5));
System.out.println("Factorial of 10 is " + factorial(arr, 10));
System.out.println("Factorial of 20 is " + factorial(arr, 20));
System.out.println("Factorial of 15 is " + factorial(arr, 15));
*/
}

}






Explanation / Answer

Please rate with 5 stars :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote