Thread: Binary search

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Always?
    So now it's guessing time?

    Did you run the program with your debugger and single step though this section of your code watching the values of the variables as you step?

    By the way if your guess was correct the program should finish, does it?

    Jim

  2. #17
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    It finishes when I introduce a value that IS in the vector. When I introduce a value that IS NOT, then it does not.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When I introduce a value that IS NOT, then it does not.
    So you see a problem, what have you done to find the solution to this problem? I've given you a hint as to what is wrong and one way of seeing for yourself what is happening. Have you tried your debugger? Did you go back and review your documentation for your binary search routine?

    Jim

  4. #19
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by OctavianC View Post
    It finishes when I introduce a value that IS in the vector. When I introduce a value that IS NOT, then it does not.
    You have a while loop like this:

    Code:
    while (flag == 0 && inf <= supp) {
        ...
    }
    It contains the condition flag == 0 && inf <= supp. When that condition becomes false, then the loop will end.

    You have 2 end conditions for your algorithm, number one is that you find the value and number two is that you go through all elements and don't find the value. One of them loops forever, which means that flag == 0 && inf <= supp is always true when that happens.

    You need to find out why, Jim advices you to use the debugger to start to understand why, and this is excellent advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  3. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM

Tags for this Thread