java array

How to find the lowest or highest value in an array

Page 1 of 1

6 Replies - 1844 Views - Last Post: 06 November 2009 - 11:55 AM Rate Topic: -----

#1 Billionthb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 09

java array

Posted 06 November 2009 - 11:31 AM

how can I find the lowest or highest value inside of an array?
Is This A Good Question/Topic? 0
  • +

Replies To: java array

#2 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: java array

Posted 06 November 2009 - 11:35 AM

Keep 2 variables, the max and the min. Set both of those variables to the first value of the array.

Traverse the array and make the necessary comparisons to change the max or min, as needed. We won't give you code until you post yours.

This post has been edited by Locke: 06 November 2009 - 11:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Billionthb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 09

Re: java array

Posted 06 November 2009 - 11:47 AM

fair enough, also I am receiving various errors

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at SortedList.fillRandom(SortedList.java:38)
at SortedList.<init>(SortedList.java:10)
at SortedListTest.main(SortedListTest.java:13)

the code is
public class SortedList
{
	int[] list = new int[10];

	// This is the object's CONSTRUCTOR
	// It is called when the object is created
	// Your SortedList will automatically call filLRandom at creation.
	SortedList()
	{
		fillRandom();
	}

	public void display()
	{
		// Loops through the array, printing each element on a the same line
		// Each element must be seperated by a space

		// For example:

		/*
		 * 15 2 1 4 6 8 1 2
		 */
	}

	public void fillRandom()
	{
		// Fill each index position in the array with a random number
		// The number should be a POSITIVE INTEGER between 1 and 100


		// To create a random double between 0 and .99999999, use Math.random()
		// To truncate a value (round down) making it an integer, use (int)

		// Example:
		for (int x = 0;  x<= (list.length); x++)
		{
			int randomNum = (int) (Math.random() * 101);
			list[x] = randomNum;
		}
		for (int x = 0;  x<= (list.length); x++)
		{
			System.out.println((x+1)+":"+(list[x]));
		}
		// This will give you a number between 0 and 4 
	}


	public void bubbleSort()
	{
		int notSorted=1;
		// Sort the array using a bubble sort

		while (notSorted==1)
		{
			notSorted=0;
			for (int x = 0; x < (list.length); x++)
			{
				if (list[x]>list[(x+1)])
				{
					int temp=0;
					temp=list[x];
					list[x]=list[x+1];
					list[x+1]=temp;
					notSorted=1;
				}
			}	
			for (int x = 0;  x<= (list.length); x++)
			{
				System.out.println((x+1)+":"+(list[x]));
			}
		}
		// This means that you need to SWAP adjacent items if they are out of order
		// Keep repeating the process until the array is sorted
	}

	public void selectionSort()
	{
		// Sort the array using a selection sort

		// This means that you need to find the LOWEST element and put in slot 1
		// Then the SECOND LOWEST and put it in slot 2, etc
	}
}


and the tester code
public class SortedListTest 
{
	public static void main(String [] args)	
	{	
		// Write an interface for your program which allows the user to enter
		// the following commands
		
		// random		- fills the array with random numbers
		// bubble		- sorts the array using the bubble sort
		// selection	 - sorts the array using the selection sort

		// After each command the program should automatically call display
		SortedList list=new SortedList();
		list.fillRandom();
		list.bubbleSort();

	}
}



any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: java array

Posted 06 November 2009 - 11:51 AM

For your for loops, you can never go until <= list.length. You'll always get an ArrayIndexOutOfBoundsException. Use < list.length.
Was This Post Helpful? 0
  • +
  • -

#5 Billionthb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 09

Re: java array

Posted 06 November 2009 - 11:52 AM

but that would loop it 9 times instead of 10
would I do< list.length+1
?
Was This Post Helpful? 0
  • +
  • -

#6 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: java array

Posted 06 November 2009 - 11:54 AM

No, it would loop 10, since you start x at zero.

Doing < list.length + 1 is the same as <= list.length. These are both going one index too far, and you're trying to access memory that doesn't exist.

This post has been edited by Locke: 06 November 2009 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 Billionthb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 09

Re: java array

Posted 06 November 2009 - 11:55 AM

ok, thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1