//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Problem 4.04 on page 85
//  Recursive Function returns max of first n elements of an array

public class Pr0404
{ public static void main(String[] args)
  { double[] x = new double[10] ;
    System.out.print("x = {");
    for (int i = 0 ; i < 10 ; i++)
    {  x[i] =  Math.round(Math.random()*1000.0) / 1000.0 ;
       System.out.print(x[i]) ;
       if ( i < 9 ) System.out.print(", ");
    }
    System.out.println("}");
    for ( int n = 1 ; n <= 10 ; n++ )
      System.out.println("max(x,"+n+") = " + max(x,n) ) ;
  }
  public static double max(double[] a, int n)
  { if (n==1) return a[0];       // basis
    double m = max(a,n-1);       // recursion
    if ( a[n-1] > m )  return a[n-1];
    else               return m;
  }

}
