# Temperature analysis program

• 03-29-2011, 03:30 AM
Evii0
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.
• 03-29-2011, 03:48 AM
Junky
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```
• 03-29-2011, 04:12 AM
Evii0
yeah, I know how to calculate an average :) thanks for that, it should be very useful
• 03-29-2011, 04:37 AM
pbrockway2
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.
• 03-29-2011, 04:41 AM
Junky
[cough]
second and third lines
[/cough]
• 03-29-2011, 06:31 AM
SHE
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.
.
.

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 ;     } }```
• 03-29-2011, 06:58 AM
pbrockway2
Quote:

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

A thousand apologies!
• 03-29-2011, 07:06 AM
Junky
Quote:

Originally Posted by SHE
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.
• 03-29-2011, 09:05 AM
SHE
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 ^^,,