1. Member
Join Date
Mar 2011
Posts
4
Rep Power
0

## 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. Java Code:
```loop {
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. Member
Join Date
Mar 2011
Posts
4
Rep Power
0
yeah, I know how to calculate an average :) thanks for that, it should be very useful

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
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. [cough]
second and third lines
[/cough]

6. Member
Join Date
Mar 2011
Posts
27
Rep Power
0
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);
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 07:34 AM.

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Originally Posted by Junky
[cough]
second and third lines
[/cough]

A thousand apologies!

8. 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.

9. Member
Join Date
Mar 2011
Posts
27
Rep Power
0
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 01:40 PM.

#### Posting Permissions

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