Results 1 to 7 of 7
 11122011, 11:21 AM #1Member
 Join Date
 Nov 2011
 Posts
 4
 Rep Power
 0
Need help debugging Binary search program
Hello,
I wrote a program on binary search and I never successfully complied the program to further troubleshoot the logic... Please help me out... I spend couple of hours figuring out what went wrong and I am still unsuccessful in troubleshooting the problem... I feel that I am not using correct syntax of calling array arguments in the main function...
I am pasting the entire program... The logic of the program says the user thinks of secret number in his mind within the range 1100 and computer will try to guess the number using binary search algorithm and it will ask the user to type s if the computer guessed number is smaller than user guessed number or type b if the computer guessed number is bigger than user guessed number or type c if the computer guessed number matches the user guessed number.

Output will look like
Is it 50? (type s,b or c): s
Is it 88? (type s,b or c): b
Is it 69? (type s,b or c): b
Is it 59? (type s,b or c): c
I got it after making 4 guesses

Many Thanks,
Maria
/*Program*/
import java.util.*;
import java.lang.*;
public class Binsearch
{
public static void main(String[] args)
{
System.out.println("****************************** ************************************************** ************");
System.out.println("Welcome to Guessing Number Game");
System.out.println("This program has you, the user, choose a number between 1 and 100.");
System.out.println("Then I the computer,will try my best to guess it");
System.out.println("If I guess a number that's SMALLER than the secret number, respond by typing letter s");
System.out.println("If I guess a number that's BIGGER than the secret number, respond by typing letter b");
System.out.println("If I guess a number that's CORRECT and matches the secret number, respond by typing letter c");
System.out.println("****************************** ************************************************** ************");
Bsearch(numb,guess,low,up);
}
public static int Bsearch(int[] number, int guessvalue, int lowerlimit, int upperlimit)
{
number = new int [100];
Random raIntegers1 = new Random();
guessvalue = raIntegers1.nextInt (101);
System.out.println("The computer guessed Random number between 1100 is " + guessvalue);
System.out.println("Is it " + guessvalue + "? " + "(type s, b, or c):");
char useroption;
Scanner scan = new Scanner( System.in );
useroption= scan.next().charAt(0);
do
{
int middlelimit = ((lowerlimit + upperlimit)/2);
if (number[middlelimit] == guessvalue)
{
return middlelimit;
}
else if (number[middlelimit]> guessvalue)
{
middlelimit = middlelimit 1;
return Bsearch(number, guessvalue, lowerlimit, middlelimit  1);
}
else if (number[middlelimit]<guessvalue)
{
middlelimit = middlelimit +1;
return Bsearch(number, guessvalue, middlelimit + 1,upperlimit);
}
} while (useroption=='c');
System.out.println("I got it after making guesses");
}
}
 11122011, 03:44 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,525
 Blog Entries
 7
 Rep Power
 20
Re: Need help debugging Binary search program
You have your boundaries wrong; suppose you're searching in the interval [lo, hi) (exclusive hi); the middle element is (lo+hi)/2 of course; if the middle element is the wanted element you're ready, otherwise if the middle element is too large, you have to search the interval [lo, middle) (exclusive middle); if the middle element was too small you have to search the interval [mid+1, hi) (exclusive hi). You start your search of array a in the interval [0, a.length).
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 11122011, 06:42 PM #3Member
 Join Date
 Jul 2011
 Location
 New Delhi,India
 Posts
 56
 Rep Power
 0
 11132011, 12:42 PM #4Member
 Join Date
 Nov 2011
 Posts
 4
 Rep Power
 0
Re: Need help debugging Binary search program
Modified code doesnt work...please help... thanks
 11132011, 12:56 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,525
 Blog Entries
 7
 Rep Power
 20
Re: Need help debugging Binary search program
Have you read my reply (reply #2)? Or was it written in invisible ink again?
kind regards,
Jos
ps. don't just copy and paste code and don't complain if it doesn't work; at least tell the author (and others) what exactly fails to work.cenosillicaphobia: the fear for an empty beer glass
 11132011, 09:21 PM #6Member
 Join Date
 Nov 2011
 Posts
 4
 Rep Power
 0
Re: Need help debugging Binary search program
If you can please help me out... I understand the logic of binary search and thatswhat you exactly mentioned in your statement... The problem I am facing is implementing the logic in code...
Also I am posting these questions in "New to Java forum" so I believe its expected to get some basic help...
Also I mentioned in the beginning I am not able to call array correctly so program didnt compile to further troubleshoot the logic... All I need is the program compilation and I will further troubleshoot the problem...
As a moderator your reply shows some attitude and I suggest you keep this reply concise to the point
 11132011, 09:37 PM #7
Similar Threads

Binary Search
By cengho in forum Java AppletsReplies: 4Last Post: 12242010, 10:26 AM 
Binary Search Help
By Plissken in forum New To JavaReplies: 2Last Post: 03132010, 10:34 AM 
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12102009, 01:42 AM 
binary search
By tranceluv in forum New To JavaReplies: 10Last Post: 01142008, 07:13 PM 
problem with recursive binary search program
By imran_khan in forum New To JavaReplies: 3Last Post: 08022007, 03:08 PM
Bookmarks