Results 1 to 9 of 9
  1. #1
    Evii0 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Temperature analysis program

    Hi,
    I need to write a program that reads a list of numbers from the user and prints out the minimum, maximum, and average of the values.
    and I'm not sure how to go about it, except that I have to use at least one while loop.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    loop {
        read value
        add value to total
        increase counter
        if value is less than min {
            change min
        }
        if value is greater than max
            change max
        }
    }
    calculate average (surely you should know how to do this)
    display results

  3. #3
    Evii0 is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    yeah, I know how to calculate an average :) thanks for that, it should be very useful

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    In the loop as well as keeping track of the minimum and maximum, you should also keep track of the sum and the number of items in the list. The last two quantites are useful when finding the average.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    [cough]
    second and third lines
    [/cough]

  6. #6
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    i recommend to create three methods :

    One for count max !
    second to count min !
    third to count the average !
    // I used system.in .. you can make simple modification of you want it read from specif file.
    .
    .

    Java Code:
            Scanner input = new Scanner(System.in);
            System.out.println("Please Enter the numbers");
            double ar[] = new double[5];
            double temp =0;
            int index=0;
            for(index=0;index<ar.length;++index)
            {
                temp = input.nextDouble();
                ar[index]=temp;
            }
            System.out.println("The Max is"+findMax(ar));
            System.out.println("The Average is"+findAvg(ar));
        }
    
        public static double findMax(double arr[])
        {
            double max = 0;
            for(int index =0;index<arr.length;index++)
            {
                if(arr[index]> max)
                max = arr[index];
            }
            return max ;
        }
    
           public static double findMin(double arr[])
        {
            double min = 0;
            for(int index =0;index<arr.length;index++)
            {
    
            }
            return min ;
        }
    
    
         public static double findAvg(double arr[])
        {
            int count = 0;
            double sum=0;
            double avg = 0;
            for(int index =0;index<arr.length;index++)
            {
    
            }
            
            return avg ;
        }
    }
    Last edited by SHE; 03-29-2011 at 06:34 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Junky View Post
    [cough]
    second and third lines
    [/cough]

    A thousand apologies!

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by SHE View Post
    i recommend to create three methods :
    Why???

    Your code requires it to loop over all the values 4 times. In my pseudocode above it can all be done in a single loop.

  9. #9
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    yeah i know that ! but this place to learn .. so i think it's better to learn many ways rather than one way =) .. ,,
    PS: he also can solve this by using recursion ^^,,
    Last edited by SHE; 03-29-2011 at 12:40 PM.

Similar Threads

  1. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  2. Reading a temperature sensor in Java
    By michaelwest in forum New To Java
    Replies: 2
    Last Post: 02-23-2010, 03:59 AM
  3. NetBeans Profiler and memory analysis
    By soheil in forum Advanced Java
    Replies: 0
    Last Post: 08-02-2009, 05:32 PM
  4. temperature program with inputs
    By JingGong in forum New To Java
    Replies: 3
    Last Post: 10-17-2008, 06:34 PM
  5. Temperature converstion
    By jamesov89 in forum New To Java
    Replies: 6
    Last Post: 09-29-2008, 04:51 AM

Posting Permissions

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