Array Max and Min Guidance Needed

Just need a separate pair of eyes to help me figure out what is wrong

Page 1 of 1

3 Replies - 1140 Views - Last Post: 27 October 2009 - 09:57 AM Rate Topic: -----

#1 Luckless  Icon User is offline

  • </luck>
  • member icon

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

Array Max and Min Guidance Needed

Post icon  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! :D

Is This A Good Question/Topic? 0
  • +

Replies To: Array Max and Min Guidance Needed

#2 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • 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).
Was This Post Helpful? 1
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1