/*
   ArrayExamples.java
   Burton Rosenberg
   Wed Apr  8 11:20:50 EDT 1998

*/

import java.io.* ;
import java.util.* ;

public class ArrayExamples
extends Object {

    static int findMax( int [] a, int aSize ) {
    /* given an array a, returns the maximum value in
       the array. Returns 0 if the array is empty.
    */

        // first, guard against bad input.
        if ( a==null || aSize==0 ) return -Integer.MIN_VALUE ;  
  
        int maxSoFar = a[0] ;
        for ( int i=1; i<aSize; i++ )
           if ( a[i]>maxSoFar ) maxSoFar = a[i] ;
        return maxSoFar ;
    }
    
    static int findMatch( int [] a, int aSize, int b ) {
    /* given an array a and an integer b, find
       i such that a[i]==b. If no such i exists,
       return -1.
    */
        // first, guard against bad input.
        if ( a==null ) return -1 ; 

        for ( int i=0; i<aSize; i++ ) {
            if ( a[i]==b ) return i ;
        }
        return -1 ;
    }

    static int countMatches( int [] a, int aSize, int b ) {
    /* given an array a and an integer b, count the
       i such that a[i]==b.
    */
        // first, guard against bad input.
        if ( a==null ) return -1 ; 

        int count = 0 ;
        for ( int i=0; i<aSize; i++ ) {
            if ( a[i]==b ) count++ ;
        }
        return count ;
    }

    // this doesn't matter, any old driver
    // code to test the subroutines.

    static BufferedReader stdin = new BufferedReader
       (new InputStreamReader(System.in) ) ;

    public static void main(String [] argv)
    throws IOException
    {
       final int SIZE = 10 ;
       final int TOFIND = 0 ;
       int [] myArray = new int[SIZE] ;
       int myArraySize ;

       System.out.println(
           "Enter up to "+ 
           SIZE + " integers terminated by an empty line.") ;

       // See page 128, class text.
       StringTokenizer tokenedInput ;  
       int i = 0 ;
       while ( i<SIZE ) { 
          tokenedInput = new StringTokenizer( stdin.readLine() ) ;
          if ( tokenedInput.countTokens()==0 ) break ; 
          while ( i<SIZE ) {
            if ( tokenedInput.countTokens()==0 ) break ; 
            myArray[i++] = Integer.parseInt( tokenedInput.nextToken() ) ;
          }
       }
       myArraySize = i ;

       System.out.println("Array of size " + myArraySize ) ;
       System.out.println("Max in array " + findMax( myArray, myArraySize ) ) ;
       if ( findMatch( myArray, myArraySize, TOFIND )<0 )
          System.out.println("Value " + TOFIND + " not found." ) ;
       else
          System.out.println("Value " + TOFIND + " found " +
             countMatches( myArray, myArraySize, TOFIND ) + " times.") ;

    }
}

