public class SoccerScores  {
  /** 11/7/2018
    * This class stores winning scores for several teams
    * that play soccer. 
    * Purpose: we want to put array processing into methods.
    * 1. find max, min, average
    * 2. copying arrays
    * 3. comparing arrays
    * 4. find duplicates
    * 5. linear search (we did 3 different versions)
    * 6. find duplicates in unsorted array
    * 7. sort the array (use 2 different algorithms: Selection Sort
    * and Bubble Sort)
    */
  public static void main(String[] args) {
    // declare and initialize 
    int[] scores = {3, 4, 2, 1, 0, 0, 0, 1, 5};
    int max = findMax(scores);
    int min = findMin(scores);
    double average = calculateAverage(scores);
    System.out.println("max score is: " + max + " lowest score is: "
                         + min + ". Average is: " + average);
    int[] copyScores = new int[scores.length];
    makeCopy(scores, copyScores);
    // enhanced for loop
    for (int e: copyScores) {
         System.out.println(e + " ");
    }
    if (isEqual(scores, copyScores))
         System.out.println("The copy is identical to the original.");
    int index = linearSearch3(scores, 0);
    if (index !=-1)
      System.out.println("the first occ of 0 is at: " + index);
    else
      System.out.println(0 + " does not occur in the array.");
    int num = linearSearch(scores, 1);
    System.out.println(1 + " occurs " + num + " times.");
    
    if (linearSearch2(scores, 5))
      System.out.println("yes, at least one team got " + 5);
    selectionSort(copyScores);
    System.out.println("After sorting copyScores: ");
    for (int e: copyScores) {
         System.out.println(e + " ");
    }
  }
  /** this linear search searches for val in arr and returns the
    * number of times val occurs in the array.
    * @param arr the array to search
    * @param val is the value that you are searching for
    * @return count of how many times val occurs in array arr
    */
  public static int linearSearch(int[] arr, int val) {
    int count=0; // declare an init count
    for (int elt: arr)
      if (elt == val)
          count++;
    return count;
  }
  /** this linear search searches for val in arr and returns true
    * if val occurs in the array, false otherwise.
    * @param arr the array to search
    * @param val is the value that you are searching for
    * @return true if val occurs in array arr, false otherwise
    */
  public static boolean linearSearch2(int[] arr, int val) {
    for (int elt: arr)
      if (elt == val)
          return true;
    return false;
  }
   /** this linear search searches for val in arr and returns the
    * index of the first occurrence of val in the array.
    * @param arr the array to search
    * @param val is the value that you are searching for
    * @return integer which is the index where val occurs in array arr
    * and -1 if val does not occur
    */
  public static int linearSearch3(int[] arr, int val) {
    for (int i=0; i<arr.length; i++)
      if (arr[i] == val)
          return i;
    return -1; // val is not found in the array
  }
  /** copyScores copies the first array into the second array.
    * @param sourceArr this is the source to be copied
    * @param destArr this is the destination (comes in as an empty array)
    */
  public static void makeCopy(int[] sourceArr, int[] destArr) {
    // check if there is room in the destination array
    if (destArr.length < sourceArr.length) {
       System.out.println("destination array was too small for a copy.");
       return;
    }
    for (int i=0; i<sourceArr.length; i++) {
       destArr[i] = sourceArr[i]; 
    }
  }
  /** isEqual compares two arrays */
  public static boolean isEqual(int[] arr1, int[] arr2) {
    // check if both have same length
    if (arr1.length != arr2.length) {
       return false;
    }
    for (int i=0; i<arr1.length; i++) {
       if (arr1[i] != arr2[i]) 
         return false;
    }
    return true;
  }
  public static int findMax(int[] scores) {
    int maxSoFar=scores[0];
    for (int i=1; i<scores.length; i++)
        if (maxSoFar < scores[i])
            maxSoFar = scores[i];
    return maxSoFar;
  }
  
  public static int findMin(int[] scores) {
    int minSoFar=scores[0];
    for (int i=1; i<scores.length; i++)
        if (minSoFar > scores[i])
            minSoFar = scores[i];
    return minSoFar;
  }
  
  public static double calculateAverage(int[] scores) {
    int sum=0;
    for (int i=0; i<scores.length; i++)
         sum+=scores[i];
    return (double)sum/scores.length;
  }

  /** Selection sort repeatedly finds the minimum of the rest of
    * the array, and puts it at loc i.
    */
  public static void selectionSort(int[] arr)  {
    // outer loop: consider loc i as the running min
    for (int i=0; i<arr.length-1; i++) {
      // inner loop checks rest of the array for a smaller value
      for (int j=i+1; j<arr.length; j++) {
          if (arr[i]>arr[j])  {  // then SWAP
             int temp = arr[i];
             arr[i] = arr[j];
             arr[j] = temp;
          } // end if
      } // end inner loop for j
    } // end outer loop for i
  } // end method
}

/** Lab: write a method to find duplicates in an UNSORTED array.
  * you can either write 3 versions the way we did with linear search
  * (return true/false, return a count, return an index)
  * or you can choose one way.
  * 
  * (Recall: last class we did a lab on finding duplicates in a 
  * SORTED array.
  ****/




