2 Replies - 840 Views - Last Post: 13 May 2013 - 04:25 PM Rate Topic: -----

#1 bkgg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-May 13

Trouble getting the correct min/max to appear from an array

Posted 13 May 2013 - 04:04 PM

I'm having some trouble with an assignment I am working on. It is fairly simple just create a program that collects 12 months worth of rainfall and return the total, average, but lastly the index within the array of what month had the min and max. Any help would be greatly appreciated I am really new to Java and based this off of a different program:

Here is a sample output:

Enter the Rainfall for month 1.
1
Enter the Rainfall for month 2.
2
Enter the Rainfall for month 3.
3
Enter the Rainfall for month 4.
4
Enter the Rainfall for month 5.
5
Enter the Rainfall for month 6.
6
Enter the Rainfall for month 7.
7
Enter the Rainfall for month 8.
8
Enter the Rainfall for month 9.
9
Enter the Rainfall for month 10.
10
Enter the Rainfall for month 11.
11
Enter the Rainfall for month 12.
12
The total Rainfall for this year is: 78.0
The average for this year is: 6.5
The month with the highest amount of rain is: 0.0
The month with the lowest amount of rain is: 0.0

Here is my current code:
import java.util.Scanner;

public class Rainfall
{
   public static void main (String [] args)
   {
       double[] Rainfall = new double[12]; // The Rainfall data
       double most = getHighest(Rainfall);
       double least = getLowest(Rainfall);

       getValues(Rainfall);
       System.out.println("The total Rainfall for this year is: " + getTotal(Rainfall));
       System.out.println("The average for this year is: " + getAverage(Rainfall));
       System.out.println("The month with the highest amount of rain is: " + most);
       System.out.println("The month with the lowest amount of rain is: " + least);
    }

   /**
      getValues method 
      @return The Rainfall array filled with user input
   */
    public static void getValues(double[] array)
    {
          double input;  // To hold user input.
	  Scanner scan = new Scanner(System.in);

          for (int i = 0; i < array.length; i++)
	  {
		System.out.println("Enter the Rainfall for month " + (i + 1) + ".");
		array[i] = scan.nextDouble();
          }
    }

   /**
      getTotal method
      @return The total of the elements in
      the Rainfall array.
   */

   public static double getTotal(double[] Rainfall)
   {
      double total = 0.0;     // Accumulator

      // Accumulate the sum of the elements
      // in the Rainfall array.
      for (int index = 0; index < Rainfall.length; index++)
         //total += Rainfall[index];
         total = total + Rainfall[index];

      // Return the total.
      return total;
   }

   /**
      getAverage method 
      @return The average of the elements
      in the Rainfall array.
   */

   public static double getAverage(double[] Rainfall)
   {
      return getTotal(Rainfall) / Rainfall.length;
   }

   /**
      getHighest method
      @return The highest value stored
      in the Rainfall array.
   */

   public static int getHighest(double[] Rainfall)
   {
      double highest = Rainfall[0];
      int index_of_highest = 0;

      for (int index = 1; index < Rainfall.length; index++)
      {
         if (Rainfall[index] > highest){
            highest = Rainfall[index];
            index_of_highest = index;
         }
      }

      return index_of_highest;
   }

   /**
      getLowest method
      @returns The lowest value stored
      in the Rainfall array.
   */

   public static int getLowest(double[] Rainfall)
   {
      double lowest = Rainfall[0];
      int index_of_lowest = 0;

      for (int index = 1; index < Rainfall.length; index++)
      {
         if (Rainfall[index] < lowest){
            lowest = Rainfall[index];
            index_of_lowest = index;
         }
      }

      return index_of_lowest;
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble getting the correct min/max to appear from an array

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Trouble getting the correct min/max to appear from an array

Posted 13 May 2013 - 04:10 PM

You're calculating most and least before the Rainfall[] array has any values in it. An approach to prevent that from happening is to include the method call in the print statement:

System.out.println("The month with the highest amount of rain is: " + getHighest(Rainfall) );

Eliminating the need for the variable, most.
Was This Post Helpful? 0
  • +
  • -

#3 bkgg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-May 13

Re: Trouble getting the correct min/max to appear from an array

Posted 13 May 2013 - 04:25 PM

Thank you so much I am getting the correct output after I made the adjustment and added + 1 to compensate for the array index numbering.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1