Question

In: Computer Science

Convert the following recursive method to be tail-recursive Explain what is the advantage of a recursive...

  1. Convert the following recursive method to be tail-recursive
  2. Explain what is the advantage of a recursive method to be tail-recursive.
public int f8( int[] arr, int index )
{
   if ( index == -1 )
      return 0;
   if (arr[index] == 2)
      return 1 + f8( arr, index - 1 );
   return f8( arr, index - 1);
}

Solutions

Expert Solution

ff8() is the tail recursive method

uses of tail recursion:

uses less memory and less stack frames.

import java.util.*;

class Hello {

    public int f8(int[] arr, int index) {

        if (index == -1)

            return 0;

        if (arr[index] == 2)

            return 1 + f8(arr, index - 1);

        return f8(arr, index - 1);

    }

    public int ff8(int[] arr, int index, int count) {

        if (index == -1)

            return count;

        if (arr[index] == 2)

            return ff8(arr, index - 1, count + 1);

        return ff8(arr, index - 1, count);

    }

    public static void main(String[] args) {

        int[] a = { 1, 2, 3, 2, 5 };

        Hello h = new Hello();

        System.out.println(h.f8(a, 4));

        System.out.println(h.ff8(a, 4, 0));

    }

}


Related Solutions

What is "tail autonomy"? What is its advantage? What is Vitellogenesis? what hormones are required for...
What is "tail autonomy"? What is its advantage? What is Vitellogenesis? what hormones are required for this process?
explain the concept of competency based education. what are the advantage of this method of learning...
explain the concept of competency based education. what are the advantage of this method of learning for student who have to learn a large number of skill?
what is absolute advantage theory?? explain theory by my map method
what is absolute advantage theory?? explain theory by my map method
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive...
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive method addOddNodes for the LinkedList class which represents singly linked lists. public class LNode { private int m_info; private LNode m_link; public LNode(int info){ m_info = info; m_link = null; } public void setLink(LNode link){ m_link = link; } public LNode getLink(){   return m_link; } public void setInfo(int info){ m_info = info; } public int getInfo(){   return m_info; } } public class LinkedList {...
Question 11 (1 point) What is wrong with the following recursive method, which is meant to...
Question 11 (1 point) What is wrong with the following recursive method, which is meant to compute the sum of all numbers from 1 to n? public int summation(int n) { return n + summation(n-1);} Question 11 options: The method lacks a base case The method lacks a recursive step The base case is incorrect The recursive step is incorrect Question 12 (1 point) In which of these situations does it make sense to consider a recursive solution? Question 12...
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number...
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number in the list and swaps it with the first number. 2.Ignore the first number and sort the remaining smaller list recursively.
1. Explain the difference between the tail of a meteor and the tail of a comet?...
1. Explain the difference between the tail of a meteor and the tail of a comet? What are they composed of, how are they created, how do we see them…. ? 2. Explain the difference between luminosity and brightness. How do they relate to apparent magnitude and absolute magnitude? 3. Investigate the stellar evolution of a star. Use the brightest star from the Constellation you read about in the early HW assignment. (Or pick another well-known star) Include: Star Name...
7.Are the following scenarios one-tail or two-tail tests? Explain why for each. a) If I don’t...
7.Are the following scenarios one-tail or two-tail tests? Explain why for each. a) If I don’t have enough money to pay the bill I’m going to look like a jerk on my big date. b) I’m handing out beer coupons and will get arrested if I give one to a minor. c) I’m distributing fliers for a new teen-oriented business in town and I don’t want to waste them on members of the wrong demographic. d) You’re placing a food...
Use the place value method, the division method, or the equivalency chart to convert the following....
Use the place value method, the division method, or the equivalency chart to convert the following. You must show all steps. Convert the following decimal numbers to the hexadecimal number system. a. 7 b. 32 c. 55 d. 254 e. 512
Use the place value method, the division method, or the equivalency chart to convert the following....
Use the place value method, the division method, or the equivalency chart to convert the following. You must show all steps. Convert the following decimal numbers to the binary number system. a. 10 b. 43 c. 96 d. 156 e. 255
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT