Results 1 to 7 of 7
  1. #1
    cotemaria is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default 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");

    }

    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: Need help debugging Binary search program

    Quote Originally Posted by cotemaria View Post
    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.....

  4. #4
    cotemaria is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Need help debugging Binary search program

    Modified code doesnt work...please help... thanks

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default 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

  6. #6
    cotemaria is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default 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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,901
    Rep Power
    25

    Default Re: Need help debugging Binary search program

    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.

Similar Threads

  1. Binary Search
    By cengho in forum Java Applets
    Replies: 4
    Last Post: 12-24-2010, 11:26 AM
  2. Binary Search Help
    By Plissken in forum New To Java
    Replies: 2
    Last Post: 03-13-2010, 11:34 AM
  3. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 02:42 AM
  4. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 08:13 PM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 04:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •