# Array Max and Min Guidance Needed

Page 1 of 1

## 3 Replies - 1259 Views - Last Post: 27 October 2009 - 09:57 AMRate 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=134518&amp;s=5c7ea38312688697673d982a7d08f365&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Luckless

• </luck>

Reputation: 293
• Posts: 1,146
• Joined: 31-August 09

# Array Max and Min Guidance Needed

Posted 26 October 2009 - 10:36 AM

Hey guys, I have this assignment to take an array list given to me and find the average, minimum, and maximum number. My average method is working fine but my minValue and maxValue methods aren't working the way I want them to. Any idea what is wrong with this code? :
```
public class arrayTest {

public static int[] list = { 5, 4 , 3 , 2, 1, 0, 10, 9, 8, 7, 6};

public static void main(String[] args){

average();
maxValue();
minValue();
}

public static void average(){

double average = 0;
for (int i = 0; i < list.length; i++){

average = average + list[i];
}
average = average / 11;
System.out.println("Average: " + average);
}

public static void maxValue(){

int maxValue = list[0];
for (int i = 0; i > list.length; i++){

if (maxValue <= list[i]){
maxValue = list[i];
}
}

System.out.println("Max Value in array is: " + maxValue);
}

public static void minValue(){
int minValue = list[0];

for (int i = 0; i > list.length; i++){
if (minValue >= list[i]){
minValue = list[i];
}
}

System.out.println("Min value in array is: " + minValue);

}
}

```

Here is the program when it is compiled and run:
```Average: 5
Max Value in array is: 5
Min value in array is: 5

```

Thanks a ton!

Is This A Good Question/Topic? 0

## Replies To: Array Max and Min Guidance Needed

### #2 LaFayette

• D.I.C Regular

Reputation: 43
• Posts: 326
• Joined: 24-November 08

## Re: Array Max and Min Guidance Needed

Posted 26 October 2009 - 11:01 AM

The conditions in the for loops in both maxValue and minValue are wrong. Turn the "larger than" into "smaller than"!

This post has been edited by LaFayette: 26 October 2009 - 11:01 AM

### #3 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Array Max and Min Guidance Needed

Posted 26 October 2009 - 11:03 AM

In your for loops for min/max you have > when you mean < (in the for (... declarations).

### #4 Luckless

• </luck>

Reputation: 293
• Posts: 1,146
• Joined: 31-August 09

## Re: Array Max and Min Guidance Needed

Posted 27 October 2009 - 09:57 AM

I'm having one of those *facepalm* moments. Thanks a lot guys, I should have caught that. Just been one of those days.