Function:
public static int numPins(int num)
{
if(num == 0)
return 0;
// recursively find the number of pins in num - 1
else
return num + numPins(num - 1);
}
Sample Program:
public class Bowling
{
public static int numPins(int num)
{
if(num == 0)
return 0;
// recursively find the number of pins in num - 1
else
return num + numPins(num - 1);
}
public static void main(String[] args)
{
int i;
for( i = 0 ; i <= 10 ; i++ )
System.out.println("No of pins for num = " + i + " : " + numPins(i) + "\n");
}
}
Sample Output:
Write a recursive method that counts the number of bowling pins given the number of pins...
Need help with this. Java please! Write a recursive method that counts the number of bowling pins given the number of pins in the back row: 1. public static int numPins (int num) numPins(0)-> 0 numPins(1) → 1 numPins(2)-> 3
on 2 Consider the following recursive method test: public static int test(String s, int last) if (last < 0) { return 0; if (s.charAt(last) == 0) { return 2 - test(s, last - 1); return 1 + 2 test(s, last - 1); What is the output of: test("11001", 4). TTT Arial 3 (12pt) - TEE 25
3) Consider the following recursive method, what will be the output for the following method calls? Explain. (25 pts.) public static void sample (int number) if (number < 0) { System.out.println( superwriteVertical (-number); System.out.println(number); superwriteVertical (number/ 10); else if (number 10) else ( System.out.print1n(number % 18); sample(-100); sample(50); sample(1024);
Java Be able to write or understand simple recursive code, including code that uses divide and conquer static int calculate(int v)l System.out.println("calculate "+v); if( v<-1) return 2; return 7*calculate(v/2); What will be the output of the statement calculate(6)?
in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray (int[] array) that returns an ArrayList of all permutations of the the parameter array. The ArrayList may contain the ar- rays in any order. Example: Suppose array = {4, 7, 1, 2}. Then the ArrayList would contain the following arrays, but in any order: 4 7 12 7 4 1 2 2 1 4 7 1 24 7 4 7 2 1 7 4...
5. Given the following class definition, write Java code that implements the addFirst method public class MyLinkedList<E> { private Node<E> head, tail; private int size 0; // Number of elements in the list /** Add an element to the beginning of the list */ public void addFirst (E e) { private static class Node<E> { E element; Node<E> next; public Node (E element) { = element; this.element
Java Write a method countStairs that, given a number of blocks, counts and returns the number of stairs you could build with the blocks. Stairs are built in a staircase with one block for the first step, two for the second step, three for the third step, and so on, like this picture which shows * for each block in a staircase: * ** *** If you had 5 blocks, you could only build 2 stairs (which requires 1+2 =...
(b) Trace the following code. // Precondition: n > 0 // <method description here> public static int dothing (int n) { int ans = ; for (int x 1; x <= n; x++) ( for (int y = x; y <= n; y++) ( ans+ } return ans; } (ii) If you were asked to write a one-line description above this method, what would it be? (iii) Could you rewrite the method so that it runs more efficiently?
Given an integer x which is assumed to be in the list a, write a method that returns the position of the first occurrence of x in a. Positions are counted as 0,1,2,.... If x does not appear in the list, you should throw an IllegalArgumentException. static int find(int x, List<Integer> a) Given an integer x which is assumed to be in the list a, write a method that returns the position of the first occurrence of u in a....
This is for C in Linux: Problem 1: Given the following recursive function: void recursiveFunction( int m ) printf("%d", m); if( m <= 0 ) return; if( n + 2 == 0 ) recursiveFunction( m - 1); else recursiveFunction( m - 2); What is the output when the following functions are called with these parameters? recursiveFunction( 5 ); recursiveFunction( 10 ); recursiveFunction( 0);