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 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)
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...
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”.
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
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
Invent a recursive function and save the program in a file named " q3c.java” It should...
Invent a recursive function and save the program in a file named " q3c.java” It should not be any of the familiar ones (like fibonacci or binomial coefficients or any of those). Make one up. Make sure it is well-defined (no ambiguity), make sure it isn’t “infinitely recursive”. • Implement it in a Java program and demonstrate it with at least 7 test values. • Add necessary comments to the program to explain it. • In comments, describe your test...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT