# finding higher and lower values in arrays

Page 1 of 1

## 1 Replies - 258 Views - Last Post: 07 April 2010 - 08:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=166775&amp;s=98a317ee467ad4f1c5dc63af8ba22651&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 justin23

Reputation: 0
• Posts: 23
• Joined: 13-December 09

# finding higher and lower values in arrays

Posted 07 April 2010 - 08:32 PM

```	public double high(){
for (int i = 0; i < 30; i++){
if (temperature[i] > temperature[i+1]){
high = temperature[i];
}
else {
high = temperature[i+1];
}
return high;
}
}

public double low(){
for (int i = 0; i < 30; i++){
if (temperature[i] < temperature[i+1]){
low = temperature[i];
}
else {
low = temperature[i+1];
}
}
return low;
}

```

these are methods for returning the lowest and highest temperatures of the month, specifically months that have 31 days.
would i be able to turn this into my teacher and receive full credit or should i use a different method?

Is This A Good Question/Topic? 0

## Replies To: finding higher and lower values in arrays

### #2 erik.price

• D.I.C Lover

Reputation: 485
• Posts: 2,690
• Joined: 18-December 08

## Re: finding higher and lower values in arrays

Posted 07 April 2010 - 08:45 PM

What if the temperature array looked like this (pretend the for loop was modified to allow for this 4 day month): {-1, 2, 1, 4} (it was a particularly cold 4 day month)

On the first iteration of low's loop, low would be set to -1, since it is less than 2
On the second iteration of low's loop, low would be set to 1, since it is less than 2 //even though -1 is lower
On the third iteration of low's loop, low would be set to 1, since it is less than 4

The lowest number in the array is obviously -1, but your method reports it to be 1.

Instead of comparing temperature[i] to the next element in the array, compare it to the current value of low. You would also remove the else in this circumstance.

The same principle applies for the high method