public class ArraysExercises{

    /**
     * Displays all of the elements of an array (of doubles) on one line
     * @param array An array of values
     */
    public static void displayAll( double[] arr ){
        for( int index = 0; index < arr.length; ++index){
            System.out.println( arr[index] );
        }
    }


    /**
     * Makes a copy of the elements in array from startIndex to endIndex (but not including endIndex) (up to the end of the array)
     * @param arr The original array
     * @param startIndex The first element in arr to copy
     * @param endIndex The first element in arr past the region to copy
     * @return A copy of the requested subset from the array
     */
    public static int[] subset( int[] arr, int startIndex, int endIndex ){

        // allocate memory for the subset array
        int[] subsetArray = new int[ endIndex - startIndex ];

        // loop through arr, and somehow get the right values from arr into the subset array
        int subsetArrayIndex = 0;
        for( int i = startIndex; i < endIndex && i < arr.length; ++i){
            subsetArray[ subsetArrayIndex ] = arr[i];
            subsetArrayIndex = subsetArrayIndex + 1;
        }

        // return a reference to the subset array
        return subsetArray;
    }


    /**
     * Returns the average value
     * @param ... ...
     * @return the average value
     */
    public static double average( double[] arr ){
        double total = 0.0;
        for( int index = 0; index < arr.length; ++index){
            total += arr[index];
        }
        return total/arr.length;
    }


    public static void doubleValues( int[] arr){
        for( int i = 0; i < arr.length; ++i){
            arr[i] = arr[i]*2;
        }
    }

    public static String[] append( String[] arr, String item ){
        // create a duplicate array
        String[] dup = new String[ arr.length + 1 ];

        // copy elements from arr to the new array
        for(int i = 0; i < arr.length; ++i){
            dup[i] = arr[i];
        }

        // add in item
        dup[ dup.length - 1 ] = item;

        // set references
        return dup;
    }


    public static void main( String[] args ){

        /*
          Array Basics: Declaring and Assigning
          You want to store the number of miles you ran for each day this year.
          Declare an array to store named milesRan that can store them.
          Assume that you ran 3.1 miles the first day of the year.
          Assign that value to the first element.
        */
        double[ ] milesRan = new double[ 366 ];
        milesRan[0] = 3.1; // 5K

        /*
          Array Basics: Loops & Arrays
          Assume that you have already populated milesRan with the first 100
          days of running.
          Write Java code to calculate and display the average miles run per day
          (for the first 100 days).
        */
        // average = total / count (100)
        double total = 0.0;
        for( int index = 0; index < 100; ++index){
            total += milesRan[index];
        }
        System.out.println( "Average is: " + total/100.0);

        /*
          Array Basics: Arrays and Methods - Parameters
          Write a method that displays all of the elements of an array
          (of doubles), all on one line.
          Use that method to display all of the values in milesRan.
        */
        displayAll( milesRan );

        System.out.println( "Average for all year: " + average( milesRan) );

        /*
          Array Basics: Arrays and Methods
          Display all of the values of an int array.
          Next, pass that int array to a method that doubles each element.
          Then, display all of the values of an int array.
         */
        int[] array = {4, 1, 2, 3, 13};
        System.out.println("Before:");
        for( int i = 0; i < array.length; ++i){
            System.out.println( array[i] );
        }
        doubleValues( array );
        System.out.println("After:");
        for( int i = 0; i < array.length; ++i){
            System.out.println( array[i] );
        }

        displayAll( milesRan );



        // display each element before the method call

        // display each element after the method call


        /*
          Write a method that takes an int array, a starting index and an ending
          index. Return a copy of the array that just has the elements from the
          starting index to the ending index.
          Display each of the values in the returned array (outside of the method).
        */
        int[] sub = subset( array, 1, 2 );
        System.out.println( "Sub:");
        for( int i = 0; i < sub.length; ++i){
            System.out.println( sub[i] );
        }

        sub = subset( array, 2, 8 );
        System.out.println( "Sub:");
        for( int i = 0; i < sub.length; ++i){
            System.out.println( sub[i] );
        }


        /*
          Write a method that adds in "Liam" into the children array.
          Add a call to your method where the comment indicates, otherwise,
          do not change the code provided.
          Verify that it displays all four names.
         */

        String[] children = new String[ 3 ];
        children[0] = "Emma";
        children[1] = "Noah";
        children[2] = "Olivia";

        // insert method call here to add in Liam
        children = append( children, "Liam");

        for( String child : children ){
            System.out.println( child );
        }


    }
}