Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 5th, 2013, 05:28 PM
 blyons626
Registered User

Join Date: Mar 2013
Posts: 13
Time spent in forums: 4 h 2 m 43 sec
Reputation Power: 0
Homework - Java Binary Search

Here's the description of what I need to do for the assignment:
Write a program that creates an array of 1000 random numbers in the range 1 – 5000. Then ask the user for a value between and 5000, and search the array until you either find the value or determine that the value is not in the array. Print a message indicating whether or not the value was found, and how many numbers you had to look at before you found the value (or determined that it was not there).

The program I've written so far stops after one guess it needs to keep cycling until you guess the right number

here's my code so far:
Code:
```import java.util.*;

public class FindNumber
{
final int MaxVal = 5000;
int value;
int numbers[] = new int[1000];
FindNumber()
{
Random rg = new Random();
for (int i=0;i<numbers.length;i++)
numbers[i]=rg.nextInt(MaxVal+1);

Arrays.sort(numbers);
}
public int searchValue(int val)
{
int min=0, max = numbers.length;
int i=0;
while(true)
{
i++;
if (numbers[(max+min)/2]==val)
return i;
else if (val > numbers[(max+min)/2])
min = (max+min)/2 + 1;
else
max = (max+min)/2 - 1;
if (min==max)
return -1;

}

}

public static void main(String[] args)
{
FindNumber fn = new FindNumber();
Scanner kb = new Scanner(System.in);
System.out.println("Enter any positive integer between 1 and 5000.");
int x = kb.nextInt();
if (fn.searchValue(x)>0)
System.out.println("Found in " + fn.searchValue(x) + " tries.");
else
}
}```

does anyone have any ideas on what I could change around or fix with my code

#2
May 5th, 2013, 07:44 PM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,971
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 46 m
Reputation Power: 1283
I don't see any obvious errors. Are you sure it isn't working?

#3
May 5th, 2013, 09:00 PM
 blyons626
Registered User

Join Date: Mar 2013
Posts: 13
Time spent in forums: 4 h 2 m 43 sec
Reputation Power: 0
Yeah, it either stops after one entry or will say found in ect. amount of tries after one entry... so it's definitely not working for me

#4
May 6th, 2013, 08:52 AM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,971
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 46 m
Reputation Power: 1283
Quote:
 Originally Posted by blyons626 Yeah, it either stops after one entry or will say found in ect. amount of tries after one entry... so it's definitely not working for me

The potential problem I see if here

Code:
```            if (min==max)
return -1;```

This means you are looking at a one element segment of the array, but you're not checking to see if the number there is equal to val.

#5
May 6th, 2013, 12:21 PM
 blyons626
Registered User

Join Date: Mar 2013
Posts: 13
Time spent in forums: 4 h 2 m 43 sec
Reputation Power: 0
I changed the
Code:
```if (min==max)
return -1;```

to
Code:
```if (min>max)
return -1;```

but its still making me void static main after every guess

#6
May 6th, 2013, 12:32 PM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,971
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 46 m
Reputation Power: 1283
Quote:
 Originally Posted by blyons626 I changed the Code: ```if (min==max) return -1;``` to Code: ```if (min>max) return -1;``` but its still making me void static main after every guess

No that's not what I mean.

When min == max, that means that you are now looking at a one element segment of the original array. At that point, the number you are looking for could be at that position.

#7
May 6th, 2013, 04:06 PM
 blyons626
Registered User

Join Date: Mar 2013
Posts: 13
Time spent in forums: 4 h 2 m 43 sec
Reputation Power: 0
Quote:
 Originally Posted by bullet No that's not what I mean. When min == max, that means that you are now looking at a one element segment of the original array. At that point, the number you are looking for could be at that position.

so should I take this out of my code?

#8
May 6th, 2013, 04:22 PM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,971
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 46 m
Reputation Power: 1283
Quote:
 Originally Posted by blyons626 so should I take this out of my code?

Since you have an infinite loop, you will have to have a way to break it.

#9
May 6th, 2013, 11:47 PM
 blyons626
Registered User

Join Date: Mar 2013
Posts: 13
Time spent in forums: 4 h 2 m 43 sec
Reputation Power: 0
Yeah I still can't figure it out

#10
May 7th, 2013, 09:35 AM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,971
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 46 m
Reputation Power: 1283
Quote:
 Originally Posted by blyons626 Yeah I still can't figure it out

Just try checking the one element to see if it's the one you're looking for.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Java Binary Search