Question

In: Computer Science

Write a recursive function that finds the minimum value in an ArrayList. Your function signature should...

Write a recursive function that finds the minimum value in an ArrayList.

Your function signature should be

public static int findMinimum(ArrayList<Integer>)

One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”.

For example, if you have the ArrayList

[1, 3, 2, 567, 23, 45, 9],

the minimum value in this ArrayList is either 9 or the minimum value in [1, 3, 2, 567, 23, 45]


Hint:
The trick is to remove the last element each time to make the ArrayList a little shorter.

import java.util.*;

public class RecursiveMin
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
  
ArrayList<Integer> numbers = new ArrayList<Integer>();
  
while (true){
System.out.println("Please enter numbers. Enter -1 to quit: ");
int number = input.nextInt();

if (number == -1){
break;
}
else {
numbers.add(number);
}
}

int minimum = findMinimum(numbers);
System.out.println("Minimum: " + minimum);
}
  
public static int findMinimum(ArrayList<Integer> numbers)
{
  
// Base Case: What is the smallest ArrayList you can have?
// What is the minimum value of that array?
  
// Recursive call: How do you find the minimum of the rest of the ArrayList?
// (Not including the last element)
  
// Return: The minimum of (the last element, minimum of the rest of the ArrayList)
  
}
}

Solutions

Expert Solution

import java.util.*;

public class RecursiveMin
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        ArrayList<Integer> numbers = new ArrayList<Integer>();

        while (true){
            System.out.println("Please enter numbers. Enter -1 to quit: ");
            int number = input.nextInt();

            if (number == -1){
                break;
            }
            else {
                numbers.add(number);
            }
        }

        int minimum = findMinimum(numbers);
        System.out.println("Minimum: " + minimum);
    }

    public static int findMinimum(ArrayList<Integer> numbers)
    {
        if(numbers.size()==1){
            return numbers.get(0);
        }
        else{
            int min = findMinimum(new ArrayList(numbers.subList(1, numbers.size())));
            if(min > numbers.get(0)){
                return numbers.get(0);
            }
            else{
                return min;
            }
        }
    }
}


Related Solutions

a) Write an function that finds the independent variable value at which a mathematical function is...
a) Write an function that finds the independent variable value at which a mathematical function is maximized over a specified interval. The function must accept a handle to a function, and evaluate that function at values ranging from x1 to x2 with an increment of dx. The value returned is the value of x at which the maximum value of f(x) occurs. Function syntax: xm = xmax(f,x1,x2,dx); As was the case in the previous problem, this function does not find...
/** * Write a recursive function that accepts a Queue<Integer>. It * should change every int...
/** * Write a recursive function that accepts a Queue<Integer>. It * should change every int in this queue to be double its original * value. You may NOT use loops or any other data structures besides * the queue passed in as a parameter. You may use a helper function. * @param q */ public static void doubleElements(Queue<Integer> q) {}
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
it should be c++ data structure Write a recursive function that draws the following shape. X...
it should be c++ data structure Write a recursive function that draws the following shape. X XXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXX XXXXXXX XXXXX XXX X The length of the longest row in the shape and the shape's character are function parameters. In above shape the longest row is 9 and the pattern's character is "X”.
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT