# problem with min max

• 04-03-2012, 05:39 AM
maximumammo
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!

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, 06:26 AM
mwr1976
Re: problem with min max
you did not say the problem you are having.
• 04-03-2012, 06:36 AM
maximumammo
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, 07:14 AM
mwr1976
Re: problem with min max
first look at these two loops from your code:

1.
Code:

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

```int minIndex = 0; for(int i=0; i > tempereture.length ; i++) {     if(tempereture[i] > tempereture[minIndex])         minIndex = i; }```
it looks to me like you are confusing the loop condition with the condition for finding the minIndex and max Index

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.
Code:

``` if(tempereture[i] > tempereture[maxIndex])   maxIndex = i;```
2.
Code:

```if(tempereture[i] > tempereture[minIndex])         minIndex = i;```
you minIndex is actually equal to your max at this point. Just because you call it min doesn't make it min.

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

Code:

```int daysOverZero, daysUnderZero; for(int i = 0; i < tempereture.length; i++) {     if (tempereture[i] < 0 )     {         daysUnderZero++;       }     else       daysOverZero++; }```
• 04-03-2012, 09:31 PM
maximumammo
Re: problem with min max
Quote:

Originally Posted by mwr1976
first look at these two loops from your code:

1.
Code:

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

```int minIndex = 0; for(int i=0; i > tempereture.length ; i++) {     if(tempereture[i] > tempereture[minIndex])         minIndex = i; }```
it looks to me like you are confusing the loop condition with the condition for finding the minIndex and max Index

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.
Code:

``` if(tempereture[i] > tempereture[maxIndex])   maxIndex = i;```
2.
Code:

```if(tempereture[i] > tempereture[minIndex])         minIndex = i;```
you minIndex is actually equal to your max at this point. Just because you call it min doesn't make it min.

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

Code:

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

O ok, thanks for all your help I really appreciate it