A two-dimensional array of double, rainfall, will be used to represent the daily rainfall for a given year. In this scheme, rainfall[month][day] represents the amount of rain on the given day and month. For example,
rainfall[1][15] is the amount of rain on Jan. 15
rainfall[12][25] is the amount of rain on Dec. 25
The array can be declared as follows:
double[][] rainfall = new double[13][32];
This creates 13 rows indexed from 0 to 12 and 32 columns indexed from 0 to 31, all initialized to 0.0. Row 0 and column 0 will be ignored. Column 31 in row 4 will be ignored, since April 31 is not a valid day. In years that are not leap years, columns 29, 30, and 31 in row 2 will be ignored since Feb. 29, 30, and 31 are not valid days.
Consider the method averageRainfall below:
/** Precondition:
* - rainfall is initialized with values representing amounts
* of rain on all valid days.
* - Invalid days are initialized to 0.0.
* - Feb 29 is not a valid day.
* Postcondition: Returns average rainfall for the year.
*/
public double averageRainfall(double rainfall[][])
{
double total = 0.0;
/* more code */
}
Which of the following is a correct replacement for
/* more code */ so that the postcondition for the method is satisfied?
I
for (int month = 1; month < rainfall.length; month++)
for (int day = 1; day < rainfall[month].length; day++)
total += rainfall[month][day];
return total / (13 * 32);
II
for (int month = 1; month < rainfall.length; month++)
for (int day = 1; day < rainfall[month].length; day++)
total += rainfall[month][day];
return total / 365;
III
for (double[] month : rainfall)
for (double rainAmt : month)
total += rainAmt;
return total / 365;
(A) None
(B) I only
(C) II only
(D) III only
(E) II and III only