public class MultidimensionalArraysExercises01{ /* 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 row = 0; row < array.length; ++row){ int numberOfColumns = array[row].length; for( int col = 0; col < numberOfColumns; ++col){ array[row][col] = value; value += step; } } } public static void populate2( double[][] array ){ double value = 0.0; double step = 0.001; for( int row = 0; row < array.length; ++row){ double[] rowArray = array[row]; int numberOfColumns = rowArray.length; for( int col = 0; col < numberOfColumns; ++col){ rowArray[col] = 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 row = 0; row < array.length; ++row){ int numberOfColumns = array[row].length; for( int col = 0; col < numberOfColumns; ++col){ total += array[row][col]; } } 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 monthI = 0; rainfall[ monthI++ ] = new double[31]; // January rainfall[ monthI++ ] = new double[28]; // February rainfall[ monthI++ ] = new double[31]; // March rainfall[ monthI++ ] = new double[30]; // April rainfall[ monthI++ ] = new double[31]; // May rainfall[ monthI++ ] = new double[30]; // June rainfall[ monthI++ ] = new double[31]; // July rainfall[ monthI++ ] = new double[31]; // August rainfall[ monthI++ ] = new double[30]; // September rainfall[ monthI++ ] = new double[31]; // October rainfall[ monthI++ ] = new double[30]; // November rainfall[ monthI++ ] = new double[31]; // December populate( rainfall ); System.out.println( "Rainfall for December 31st: " + 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. */ // DataType variableName = new DataType double[][][] rainfallCentury = new double[100][12][31]; for( int yearI = 0; yearI < rainfallCentury.length; ++yearI){ populate( rainfallCentury[yearI] ); } } }