public class MultidimensionalArraysExercises02{

    /* Two-dimensional Arrays
      2. Write a method that takes a two-dimensional array as a parameter (and
      no other parameters). Have the method populate each value in the array.
      Start with 0.0 in the first index, then 0.001 in the second, etc.
      What should the return value be? Write some code to test your method.
    */
    /**
     * Simulates data with 0.000 in the first element and 0.001 in the second, etc.
     * @param array A rectangular or ragged array
     */
    public static void populate( double[][] array ){

        double value = 0.0;
        double step = 0.001;
        for( int i = 0; i < array.length; ++i){
            // for each month
            for( int j = 0; j < array[i].length; ++j){
                // each day of the month
                array[i][j] = value;
                value += step;
            }
        }
    }
    public static void populate2( double[][] array ){
        double value = 0.0;
        double step = 0.001;
        for( int i = 0; i < array.length; ++i){
            // for each month
            double[] monthArray = array[i];
            for( int j = 0; j < monthArray.length; ++j){
                // each day of the month
                monthArray[j] = value;
                value += step;
            }
        }
    }


    /* Two-dimensional Arrays
      3a. Write a method that takes a two-dimensional array as a parameter (and
      no other parameters). Have the method return the total of all of the
      values in the array. Write some code to test your method. (If you use the
      method described above to populate each value in the rainfall array, then
      you should get a total of approximately 66.43. Note, if you're getting a
      total of 69.006, then you're probably using a rectangular array instead of
      a ragged or jagged array.)
    */

    /**
     * Calculates the total of all of the values
     * @param array A rectangular or ragged array
     * @return The sum of all of the values in array
     */
    public static double total( double[][] array ){
        double total = 0.0;

        for( int i = 0; i < array.length; ++i){
             for( int j = 0; j < array[i].length; ++j){
                 total += array[i][j];
             }
        }

        return total;
    }

    public static void main( String[] args ){

        /* Two-dimensional Arrays
        1. Declare a two-dimensional array that can hold the amount of rainfall
        for everyday in a year. The first index should be the month and the
        second index the day of the month.
        */
        double[][] rainfallRectangular = new double[12][31];
        double[][] rainfall = new double[12][]; // ragged array
        int monthIndex = 0;
        rainfall[ monthIndex++ ] = new double[31]; // January
        rainfall[ monthIndex++ ] = new double[28]; // February
        rainfall[ monthIndex++ ] = new double[31]; // March
        rainfall[ monthIndex++ ] = new double[30]; // April
        rainfall[ monthIndex++ ] = new double[31]; // May
        rainfall[ monthIndex++ ] = new double[30]; // June
        rainfall[ monthIndex++ ] = new double[31]; // July
        rainfall[ monthIndex++ ] = new double[31]; // August
        rainfall[ monthIndex++ ] = new double[30]; // September
        rainfall[ monthIndex++ ] = new double[31]; // October
        rainfall[ monthIndex++ ] = new double[30]; // November
        rainfall[ monthIndex++ ] = new double[31]; // December

        populate( rainfall );

        System.out.println( "Last rainfall amount: " + rainfall[ 11 ][ 30 ] );

        /* Two-dimensional Arrays
          3b. Write some code to test your method.
          (If you use the method described above to populate each value in the
          rainfall array, then you should get a total of approximately 66.43.
          Note, if you're getting a total of 69.006, then you're probably using
          a rectangular array instead of a ragged or jagged array.)
        */

        double totalRainfall = total( rainfall );

        System.out.println( "Total rainfall: " + totalRainfall );



        /* Multidimensional Arrays
          1. Declare a multi-dimensional array that can hold the amount of
          rainfall for everyday in a year for 100 years. The first index should
          be the year, the second index the month and the third index the day of
          the month. Will the methods described above work in this three-
          dimensional array? Discuss with someone else why or why not.
        */
        double[][][] rainfallCentury = new double[100][12][31];
        double[][] rainfallRagged = new double[100][12][]; // ragged array
        for( int yearI = 0; yearI < rainfallRagged.length; ++yearI){
            int monthIndex = 0;
            rainfallRagged[yearI][ monthIndex++ ] = new double[31]; // January
            rainfallRagged[yearI][ monthIndex++ ] = new double[28]; // February
            rainfallRagged[yearI][ monthIndex++ ] = new double[31]; // March
            rainfallRagged[yearI][ monthIndex++ ] = new double[30]; // April
            rainfallRagged[yearI][ monthIndex++ ] = new double[31]; // May
            rainfallRagged[yearI][ monthIndex++ ] = new double[30]; // June
            rainfallRagged[yearI][ monthIndex++ ] = new double[31]; // July
            rainfallRagged[yearI][ monthIndex++ ] = new double[31]; // August
            rainfallRagged[yearI][ monthIndex++ ] = new double[30]; // September
            rainfallRagged[yearI][ monthIndex++ ] = new double[31]; // October
            rainfallRagged[yearI][ monthIndex++ ] = new double[30]; // November
            rainfallRagged[yearI][ monthIndex++ ] = new double[31]; // December
        }
        for( int yearI = 0; yearI < rainfallRagged.length; ++yearI){
            populate( rainfallRagged[yearI] );
        }

    }
}