# Need help debugging Binary search program

• 11-12-2011, 12:21 PM
cotemaria
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 1-100 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 1-100 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");

}

}
• 11-12-2011, 04:44 PM
JosAH
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,

Jos
• 11-12-2011, 07:42 PM
Cosmos
Re: Need help debugging Binary search program
Quote:

Originally Posted by cotemaria
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 1-100 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 1-100 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);
// use Bsearch(number, guessvalue, lowerlimit, middlelimit); }
else if (number[middlelimit]<guessvalue)
{
middlelimit = middlelimit +1;
return Bsearch(number, guessvalue, middlelimit + 1,upperlimit); //use Bseach(number,guessvalue,middlelimit,upperlimit)
}
} while (useroption=='c');

System.out.println("I got it after making guesses");

}

}

maybe the modified code will work.....
• 11-13-2011, 01:42 PM
cotemaria
Re: Need help debugging Binary search program
• 11-13-2011, 01:56 PM
JosAH
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.
• 11-13-2011, 10:21 PM
cotemaria
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
• 11-13-2011, 10:37 PM
Norm
Re: Need help debugging Binary search program
Quote:

program didnt compile to further troubleshoot the logic... All I need is the program compilation
If you are getting compiler errors, please copy and paste them here.