In: Computer Science
Write a method named minimumValue, which returns the smallest value in an array that is passed (in) as an argument. The method must use Recursion to find the smallest value. Demonstrate the method in a full program, which does not have to be too long regarding the entire code (i.e. program). Write (paste) the Java code in this word file (.docx) that is provided. (You can use additional pages if needed, and make sure your name is on every page in this word doc.) Use the following array to test the method: int[] seriesArray = {-5, -10, +1999, +99, +100, +3, 0, +9, +300, -200, +1, -300} Make sure that the seriesArray only tests for values, which are positive, given your recursive method.
// Recursive Java program to find minimum of integer array
import java.util.*;
public class recursion_min {
// function to return minimum element using recursion
public static int findMinRec(int seriesArray[], int n)
{
// if n = 0 means whole array has been traversed
if(n == 1)
return seriesArray[0];
return Math.min(seriesArray[n-1], findMinRec(seriesArray, n-1));
}
//Main program
public static void main(String args[])
{
//initialize array
int seriesArray[] = {-5, -10, +1999, +99, +100, +3, 0, +9, +300, -200, +1, -300};
//get length of array
int n = seriesArray.length;
// Function calling
System.out.println(findMinRec(seriesArray, n));
}
}
Explanation:
1) Above is recursive Java program to find minimum of integer array
1) Copy above java program in java editor, build and run.
2) Below is test output
$javac recursion_min.java
$java -Xmx128M -Xms16M recursion_min
-300