Write a program to track and display rainfall for each month for 5 years (2010 - 2014).
Use a 2 dimensional array to store the rainfall for each month (5 rows (for the years) and 12 columns (for the months)).

Using an initializer list, populate the array with the below data.
Display the table of the rainfall values for the 5 years and 12 months (as shown below). Then, total and display the rainfall for each year, and calculate and display the average rainfall for each month.
Hint: When totaling the rainfall for each month (to calculate the average), use an outer loop to loop through the months first, and an inner loop to loop through the years.

There is nothing entered by the user.

Exactly how output should look:

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2010 0.88 1.11 2.01 3.64 6.44 5.58 4.23 4.34 4.00 2.05 1.48 0.77
2011 0.76 0.94 2.09 3.29 4.68 3.52 3.52 4.82 3.72 2.21 1.24 0.80
2012 0.67 0.80 1.75 2.70 4.01 3.88 3.72 3.78 3.55 1.88 1.21 0.61
2013 0.82 0.80 1.99 3.05 4.19 4.44 3.98 4.57 3.43 2.32 1.61 0.75
2014 0.72 0.90 1.71 2.02 2.33 2.98 2.65 2.99 2.55 1.99 1.05 0.92

Total rainfall for each year:
2010 xx.xx
2011 xx.xx
2012 xx.xx
2013 xx.xx
2014 xx.xx

Average rainfall for each month:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
x.xx x.xx x.xx x.xx x.xx x.xx x.xx x.xx x.xx x.xx x.xx x.xx

Respuesta :

Answer:

  1. public class Main {
  2.    public static void main (String [] args) {
  3.        double rainFall [][] = {
  4.                {0.88, 1.11, 2.01, 3.64, 6.44, 5.58, 4.23, 4.34, 4.00, 2.05, 1.48, 0.77},
  5.                {0.76, 0.94, 2.09, 3.29, 4.68, 3.52, 3.52, 4.82, 3.72, 2.21, 1.24, 0.80},
  6.                {0.67, 0.80, 1.75, 2.70, 4.01, 3.88, 3.72, 3.78, 3.55, 1.88, 1.21, 0.61},
  7.                { 0.82, 0.80, 1.99, 3.05, 4.19, 4.44, 3.98, 4.57, 3.43, 2.32, 1.61, 0.75},
  8.                {0.72, 0.90, 1.71, 2.02, 2.33, 2.98, 2.65, 2.99, 2.55, 1.99, 1.05, 0.92}
  9.        };
  10.        int year = 2010;
  11.        System.out.println("Year|\tJan\tFeb\tMar\tApr\tMay\tJun\tJul\tAug\tSep\tOct\tNov\tDec");
  12.        for(int i=0; i < rainFall.length; i++){
  13.            System.out.print(year+ " \t");
  14.            for(int j=0; j < rainFall[i].length; j++){
  15.                System.out.print(rainFall[i][j] + " ");
  16.            }
  17.            System.out.println();
  18.            year++;
  19.        }
  20.        double total[] = new double[5];
  21.        for(int i=0; i < rainFall.length; i++){
  22.            total[i] = 0;
  23.            for(int j=0; j < rainFall[i].length; j++){
  24.                total[i] += rainFall[i][j];
  25.            }
  26.        }
  27.        DecimalFormat df = new DecimalFormat("0.00");
  28.        year = 2010;
  29.        System.out.println("Total rainfall for each year:");
  30.        for(int i=0; i < total.length; i++){
  31.            System.out.println(year + " " + df.format(total[i]));
  32.            year++;
  33.        }
  34.        double avgRain[] = new double[12];
  35.        for(int j=0; j < 12; j++){
  36.            double totalMonthRain = 0;
  37.            for(int i = 0; i < 5; i++){
  38.                totalMonthRain += rainFall[i][j];
  39.            }
  40.            avgRain[j] = totalMonthRain / 5;
  41.        }
  42.        System.out.println("Average rainfall for each month:");
  43.        System.out.println("Jan\t\tFeb\t\tMar\t\tApr\t\tMay\t\tJun\t\tJul\t\tAug\t\tSep\t\tOct\t\tNov\t\tDec");
  44.        for(int i=0; i < avgRain.length; i++){
  45.            System.out.print(df.format(avgRain[i]) + "\t");
  46.        }
  47.    }
  48. }

Explanation

Note: The first line of import statement is missing as Brainly detect it as invalid text. Please refer to attached file to get the full source codes.

Line 10 - 11

Initialize the rainFall array with the sample data

Line 15 - 20

Use double for loop to traverse each item in the rainFall array to display the rain fall data for 12 month in 5 years.

Line 25 - 40

Calculate the total rain fall for each year and display them using a for loop

Line 42 - 56

Calculate the total monthly rain fall for each year and divide it by 5 to get the average rain fall for every month and display it using a for loop.

Ver imagen teobguan2019