Hope someone can help me! Thanks! Java Language: 1. Write a recursive method to
ID: 3698259 • Letter: H
Question
Hope someone can help me! Thanks!
Java Language:
1.
Write a recursive method to print every other element in an array, starting with the element in position 0.
The method header is: public void printEveryOther(int[] nums)
2.
{ 0 n=0
F(n)={ 1 n=1
{ F(n-1)+ F(n-2) n>1
How many calls are needed to recursively calculate the Fibonacci value of 7?
In other words, how many calls to F are needed to calculate F(7)?
3.
An infinite loop and an infinite recursion
Select one:
a. are different because it is impossible to detect the latter, while it’s quite easy to detect the former
b. both will be caught by the Java Virtual Machine during execution
c. both will be caught by the compiler
d. both continue to repeat indefinitely
Thank you again!
Explanation / Answer
1)
I have written the java code for the same:
public void printEveryOther(int[] nums)
{
static int i=0;
System.out.println(nums[i]);
if(i==nums.length)
{
return;
}
else
{
i=i+2;
printEveryOther(nums);
}
}
2)
for f(7) it will require 13 calls for function f. for n>2 the function is called twice.
3)
the correct option is a. infinite recursion will be caught as it will cause stack overflow issue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.