Results 1 to 5 of 5
  1. #1
    maximumammo is offline Banned
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default 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;
    }
    
    
    
    }}

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: problem with min max

    you did not say the problem you are having.

  3. #3
    maximumammo is offline Banned
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default 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

  4. #4
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default 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;
     
    }
    2.
    Java 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.
    Java Code:
     if(tempereture[i] > tempereture[maxIndex])
      maxIndex = i;
    2.
    Java 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:

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

  5. #5
    maximumammo is offline Banned
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: problem with min max

    Quote Originally Posted by mwr1976 View Post
    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;
     
    }
    2.
    Java 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.
    Java Code:
     if(tempereture[i] > tempereture[maxIndex])
      maxIndex = i;
    2.
    Java 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:

    Java 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •