//IntegerArrayMaxValue.java
/*
    Finds the maximum value in an array of integers.
    Uses a recursive function which applies one recursive call
    to a portion of the array that becomes smaller and smaller.
*/

public class IntegerArrayMaxValue
{
    public static void main(String[] args)
    {
        System.out.println
        (
            "\nThis program illustrates the application "
            + "of recursion to an array.\n"
        );

        int[] a1 = {5, -6, 12, 43, 17, -3, 29, 14, 35, 4};
        System.out.println
        (
            "Maximum value in the first array:  "
            + maxArrayValue(a1, 0, 9)
        );

        int[] a2 = {-5};
        System.out.println
        (
            "Maximum value in the second array: "
            + maxArrayValue(a2, 0, 0)
        );

        int[] a3 = {1, 3, 5, 7, 9, 11};
        System.out.println
        (
            "Maximum value in the third array:  "
            + maxArrayValue(a3, 0, 5)
        );

        int[] a4 = {26, 24, 22, 20, 16, 12, 0};
        System.out.println
        (
            "Maximum value in the fourth array: "
            + maxArrayValue(a4, 0, 6)
        );
    }

    public static int maxArrayValue
    (
        int a[],
        int first,
        int last
    )
    /**
        Compute the maximum value from the index interval
        [first, last] in the array.
        @return The maximum value from the interval [first, last].
        @param a An array of integers.
        @param first The lower index of the interval to be examined.
        @param last The upper limit of the interval to be examined.
        <p>Pre<p>The array a has been initialized, and
        0 <= first <= last <= (number of elements in a) - 1.
        <p>Post<p>No side effects.
    */
    {
        if (first == last)
            return a[first];
        else
        {
            int maxOfRest = maxArrayValue(a, first + 1, last);
            return (a[first] > maxOfRest) ? a[first] : maxOfRest;
        }
    }
}

