Results 1 to 8 of 8
Thread: Binary search
 11202011, 06:09 PM #1Member
 Join Date
 Oct 2011
 Posts
 14
 Rep Power
 0
Binary search
Hello :)
I'm trying to make an interactive program (guess the number)
the idea is that i put a number in my head and the program tries to guess it.
the program should ask whether the number is less or greater than the middle of the search interval.
the number should be between 1 & 1000.
so what should the psuedo code be ?
Thanks!!
thirdage.

Re: Binary search
 11202011, 06:58 PM #3Member
 Join Date
 Oct 2011
 Posts
 14
 Rep Power
 0
Re: Binary search
I'am just not getting it !!
Java Code:public class guessNumber { public static void main(String[] args) { System.out.print("Is your number between 1 and 1000 ?"); int start=1; int end=1000; int middle =(((endstart)/2)+start); int oldmiddle = 0; boolean numfound = false; while (!numfound) { boolean usersAnswer = StdIn.readBoolean(); if (usersAnswer ==true) { ifTrue(middle,start); oldmiddle = (((end middle)/2)+middle); middle = (((middlestart)/2)+start); } else { ifFalse(oldmiddle,start); } } } public static void ifTrue(int mid,int start) { System.out.print("is your number less than "+mid+" ?"); } public static void ifFalse(int mid,int start) { System.out.print("is your number less than "+mid+" ?"); } }
 11202011, 07:08 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,962
 Blog Entries
 7
 Rep Power
 22
Re: Binary search
In a range lo ... hi, you pick an element in the middle: lo ... mid ... hi; if the guess is correct, you're done; otherwise if your guess is too high, pick the interval lo ... mid1, otherwise pick the interval mid+1 ... hi.
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 11202011, 07:44 PM #5Member
 Join Date
 Oct 2011
 Posts
 14
 Rep Power
 0
Re: Binary search
example :
am picking 40
the program asks if it's between 1 & 1000
my answer: true
between 1 & 500
: true
1&250
:true
//
//
//
1&32
: false
32 & 63
:true
then 47 & 63
:false
I mean each time the interval gets small ... and we ask whether it's between the midpoint of interval and end if false
or start and midpoint if true ... hope you get what I mean.
Thanks again!
 11202011, 07:47 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,962
 Blog Entries
 7
 Rep Power
 22
 11202011, 07:51 PM #7Member
 Join Date
 Oct 2011
 Posts
 14
 Rep Power
 0
Re: Binary search
If true:
11000 becomes [1500] where we got the midpoint of 1 & 1000.
if false after [1500] was true:
we check on [5001000]
then true
we check on [7501000] .... (where 750 = ((1000500)/2) +500).
 11202011, 07:57 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,962
 Blog Entries
 7
 Rep Power
 22
Similar Threads

Problem with binary search
By Chi in forum New To JavaReplies: 1Last Post: 03072011, 04:23 AM 
Binary Search
By cengho in forum Java AppletsReplies: 4Last Post: 12242010, 11:26 AM 
Binary Search Help
By Plissken in forum New To JavaReplies: 2Last Post: 03132010, 11:34 AM 
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12102009, 02:42 AM 
binary search
By tranceluv in forum New To JavaReplies: 10Last Post: 01142008, 08:13 PM
Bookmarks