Results 1 to 5 of 5

# Thread: problem with min max

- 04-03-2012, 04:39 AM #1Member
- Join Date
- Apr 2012
- Posts
- 13

- Rep Power
- 0

## problem with min max

So I need some help with the chunk of code that I am using to find the number of days the temp is above and below zero, I am trying to find out how to fix it, thanks!

Java Code://----------------------------------------------------------------------- import java.util.Scanner; public class Lab8 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); double[] tempereture = new double[31]; //------------------------------------------------------------------------ // populate the temperature array for(int i=0; i < tempereture.length ; i++) tempereture[i] = scan.nextDouble(); //------------------------------------------------------------------------ // find the highest temperature int maxIndex = 0; for(int i=0; i < tempereture.length ; i++) { if(tempereture[i] > tempereture[maxIndex]) maxIndex = i; } System.out.println("heighest temperature is " + tempereture[maxIndex]); //-------------------------------------------------------------------------------- // The following code is to determine the average temperature double sum = 0; for(double i=0; i < tempereture.length ; i++) sum = sum + tempereture[(int) i]; double average = sum / tempereture.length; System.out.println("The average temperature is " + average); //--------------------------------------------------------------------------------- // The following code is to determine the lowest temperature int minIndex = 0; for(int i=0; i > tempereture.length ; i++) { if(tempereture[i] > tempereture[minIndex]) minIndex = i; } System.out.println("The lowest tempereture is " + tempereture[minIndex]); //--------------------------------------------------------------------------------- // The following code is to find the number of days in which the temperature is above zero and below zero int aboveZero = 0; for(int i = 0; i != tempereture.length ; i++); { if(tempereture[i] != tempereture[aboveZero]) aboveZero = i; } int belowZero = 0; for(int i = 0; i != tempereture.length ; i++); { if(tempereture[i] != tempereture[belowZero]) belowZero = i; } }}

- 04-03-2012, 05:26 AM #2Senior Member
- Join Date
- Oct 2011
- Posts
- 106

- Rep Power
- 0

## Re: problem with min max

you did not say the problem you are having.

- 04-03-2012, 05:36 AM #3Member
- Join Date
- Apr 2012
- Posts
- 13

- Rep Power
- 0

## Re: problem with min max

for lines 63-64 and lines 70-71 it states i cannot be resolved as a variable. Also I dont think the coding is right, I dont think when i put "system.out.println( "days above zero " + aboveZero); it will give the correct result

- 04-03-2012, 06:14 AM #4Senior Member
- Join Date
- Oct 2011
- Posts
- 106

- Rep Power
- 0

## Re: problem with min max

first look at these two loops from your code:

1.

Java Code:int maxIndex = 0; for(int i=0; i < tempereture.length ; i++) { if(tempereture[i] > tempereture[maxIndex]) maxIndex = i; }

Java Code:int minIndex = 0; for(int i=0; i > tempereture.length ; i++) { if(tempereture[i] > tempereture[minIndex]) minIndex = i; }

in # 2 you are saying that your counter variable i starts at 0. Your expecting your array to have 31 values in it.

you loop condition states for as long as i is greater than 31????????. Your i variable will never increment because out of the gate your loop condition is false.

then your condition for finding thr min or max is the same.

1.

Java Code:if(tempereture[i] > tempereture[maxIndex]) maxIndex = i;

Java Code:if(tempereture[i] > tempereture[minIndex]) minIndex = i;

you cannot use != for you loop condition and you logic within your loop is wrong. to loop:

Java Code:int daysOverZero, daysUnderZero; for(int i = 0; i < tempereture.length; i++) { if (tempereture[i] < 0 ) { daysUnderZero++; } else daysOverZero++; }

- 04-03-2012, 08:31 PM #5Member
- Join Date
- Apr 2012
- Posts
- 13

- Rep Power
- 0

## Bookmarks