Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads in rainfall over a period of years, then calculates a

ID: 662654 • Letter: W

Question

Write a program that reads in rainfall over a period of years, then calculates average rainfall for each year and average rainfall over the entire period. First the program should ask the user for the number of years for which data will be averaged. Then the program will ask the user to enter the amount of rainfall per month for each of those years. For example, if the user enters 3 years, then the program will ask for 12 monthly values for each of the 3 years, or 36 values in all. Note that the prompt for each month should include the month number (1 for January, 2 for February, etc.).

After each year

Explanation / Answer

import java.util.Scanner;

public class Rainfall
{

   public static void main(String args[]) {
       Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of years");
        int n=sc.nextInt();
        int temp=0;
        double[] store=new double[12*n];
        while(temp<n)
        {
          int temp2=0;
          while(temp2<12)
          {
            double rainfall=sc.nextDouble();
            store[temp*12 +temp2]=rainfall;
            temp2++;
          }
          System.out.println("Total rainfall for the year: "+totalRainfall(store,temp,12));
          System.out.println("Average monthly rainfall for the year: "+averageMonthlyRainfall(store,temp,12));
          temp+=1;
        }
        System.out.println("Total rainfall for the period: "+totalRainfall(store,0,12*n));
          System.out.println("Average monthly rainfall for the period: "+String.format("%.3G",averageMonthlyRainfall(store,0,12*n)));
   }
   public static double averageMonthlyRainfall(double [] store, int temp,int months)
   {
    int i=0;
      double totalRainfall=0;
      while(i<months)
      {
        totalRainfall+=store[temp*12+i];
        i++;
      }
      return totalRainfall/months;
   }
   public static double totalRainfall(double [] store,int temp,int months)
   {
      int i=0;
      double totalRainfall=0;
      while(i<months)
      {
        totalRainfall+=store[temp*12+i];
        i++;
      }
      return totalRainfall;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote