/**
 * Search Exercise
 * @author YOUR NAME
 * @version 
 * Each of the search methods below are intended to return the index of the
 * target value in the array, or -1 if the value isn't in the array. For each
 * one:
 * 
 * 1. Determine what output will be produced when it is called from main.
 * 
 * 2. Determine whether the method is correct. If the method is *incorrect*
 * explain what's wrong with it.
 * 
 * 3. Once you have evaluated each method, select the one that you think is
 * best.
 *
 */
public class SearchExercise {

    public static void main(String[] args) {

        int[] data = {14, 23, 8};

        System.out.println("search1: " + search1(data, 23));
        System.out.println("search2: " + search2(data, 23));
        System.out.println("search3: " + search3(data, 23));
        System.out.println("search4: " + search4(data, 23));
        System.out.println("search5: " + search5(data, 23));
        System.out.println("search6: " + search6(data, 23));

    }

    /*
     * 1. What is the output?
     * 
     * 2. Is it correct? If not, describe the problem.
     * 
     * 
     *
     */
    public static int search1(int[] numbers, int target) {
        int result = -1;

        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == target) {
                result = i;
            } else {
                result = -1;
            }
        }
        return result;
    }

    /*
     * 1. What is the output?
     * 
     * 2. Is it correct? If not, describe the problem.
     * 
     * 
     *
     */
    public static int search2(int[] numbers, int target) {

        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == target) {
                return i;
            } else {
                return -1;
            }
        }
        return -1;
    }

    /*
     * 1. What is the output?
     * 
     * 2. Is it correct? If not, describe the problem.
     * 
     * 
     *
     */
    public static int search3(int[] numbers, int target) {

        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == target) {
                return i;
            }
        }
        return -1;
    }

    /*
     * 1. What is the output?
     * 
     * 2. Is it correct? If not, describe the problem.
     * 
     * 
     *
     */
    public static int search4(int[] numbers, int target) {
        int result = -1;

        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == target) {
                result = i;
            }
        }
        return result;
    }

    /*
     * 1. What is the output?
     * 
     * 2. Is it correct? If not, describe the problem.
     * 
     * 
     *
     */
    public static int search5(int[] numbers, int target) {

        for (int i : numbers) {
            if (i == target) {
                return i;
            }
        }
        return -1;
    }

    /*
     * 1. What is the output?
     * 
     * 2. Is it correct? If not, describe the problem.
     * 
     * 
     *
     */
    public static int search6(int[] numbers, int target) {
        int index = 0;
        int result = -1;
        boolean found = false;

        while (!found && index < numbers.length) {
            if (numbers[index] == target) {
                found = true;
                result = index;
            }
            index++;
        }
        return result;
    }
}