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 {...
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
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 binary numbers to the hexadecimal number system. a. 0000 0001 b. 0111 0010 c. 1010 0111 d. 1100 0100 e. 1111 1111
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 hexadecimal numbers to the binary number system. a. C b. 33 c. 10 d. BA e. 9DF
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT