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
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
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT