import java.util.Scanner;

public class Searching {

	/*
	 * 1. Review and run the program to get the idea of what it does.
	 * 2. Complete the method findValue at the bottom of this file.
	 * 3. Complete the method countValue at the bottom of this file.
	 */
	
	public static final int ARRAY_LENGTH = 20;
	public static final int MAX_VALUE = 10;
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		//create an array
		int[] data = createRandomArray(ARRAY_LENGTH, MAX_VALUE);
		printArray(data);
		
		//smallest
		System.out.printf("The smallest value is %d.\n", findMin(data));		
		System.out.printf("The smallest value's index is %d.\n", findMinIndex(data));
		
		//find a value
		System.out.print("Enter a value to search for: ");
		int find = input.nextInt();
		
		int index = findValue(data, find);
		if(index < 0) {
			System.out.printf("The value %d was not found.\n", find);
		}
		else {
			System.out.printf("The value %d occurs at index %d.\n", find, index);
			
		}

		//count occurrence of a value
		System.out.printf("The value %d occurs %d times.\n", find, countValue(data, find));
	} //end main
	
	
	//An example of JavaDoc comments
	/**
	 * createRandomArray
	 * Create an array of random ints, r, such that 0 <= r < max.
	 * @param numValues the length of the resulting array.
	 * @param max the exclusive upper bound for the random numbers. 
	 * @return a new array of numValues ints between 0 and max (excluding max).
	 */
	public static int[] createRandomArray(int numValues, int max) {
		int[] result = new int[numValues];
		
		for(int i = 0; i < result.length; i++) {
			result[i] = (int)(Math.random()*max);
		} //end for
		
		return result;
	} //end createRandomArray
	
	/**
	 * printArray
	 * Print out the array given as a parameter.
	 *  @param data: the array to print out
	 *  @return none
	 */
	public static void printArray(int[] data) {
		System.out.print("{");
		boolean first = true;
		
		//enhanced for loop or for-each loop
		//Go through each item, instead of each index
		for(int n: data) {
			if(first) {
				System.out.printf("%d", n);
				first = false;
			} //end if
			else {
				System.out.printf(", %d", n);
			} //end else
		}//end for
		System.out.println(" }");
		
	} //end outputArray
	
	
	
	/**
	 * findMin
	 * Find the smallest element in the array.
	 * @param  data: the array to search
	 * @return the smallest element.
	 */
	public static int findMin(int[] data) {
		
		//use the first item for the minimum
		int min = data[0];
		
		//start at index 1
		for(int i = 1; i < data.length; i++) {
			if(data[i] < min) {
				min = data[i];
			} //end if
		} //end for
		return min;
		
		
		//short version
		//return data[findMinIndex(data)];
	} //end findMin
	
	
	/**
	 * findMinIndex
	 * Find the index of the smallest element in the array.
	 * @param  data: the array to search
	 * @return the smallest element's index.
	 */
	public static int findMinIndex(int[] data) {
		//use the first item for the minimum
		int min = data[0];
		int minIndex = 0;
		
		//start at index 1
		for(int i = 1; i < data.length; i++) {
			if(data[i] < min) {
				min = data[i];
				minIndex = i;
			} //end if
		} //end for
		return minIndex;
	} //end findMinIndex
	
	
	/**
	 * findValue: search an array for a given value, returning the first index of the value.
	 * @param  data: the array to search
	 * @param  value: the value I'm searching for.
	 * @return the index of the first appearance of the value in the array, or -1 if it is not in the array
	 */
	public static int findValue(int[] data, int value) {

		//loop through all elements of the array
		for(int i = 0; i < data.length; i++) {
		// if value was found, return its index
			if(data[i] == value) {
				//found it
				return i;
			}
			//This is a mistake!
			//else {
			//	return -1;
			//}
		}
		//the value must not be in the array
		return -1;
	} //end findValue
	
	
	
	//count values
	/**
	 * countValue: count the number of times a value occurs in an array
	 * @param  data: the array to search
	 * @param  value: the value I'm searching for.
	 * @return the number of times the value occurs
	 */
	public static int countValue(int[] data, int value) {
		int count = 0;
		
		for(int i = 0; i < data.length; i++) {
			if(data[i] == value) {
				count++;
			}
		}
		
		//stub method
		return count;		
	} //end countValue
	

}
