Thread: Trying to implement a binary search algorithm, please help

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    10

    Trying to implement a binary search algorithm, please help

    So, my program picks randomly a number between [1, 100] and I'm trying to implement a binary search algorithm to find that number. The problem is, nothing happens when it compiles it only says: "-------------- Build: Debug in RandWithBinary (compiler: GNU GCC Compiler)---------------

    Target is up to date.
    Nothing to be done (all items are up-to-date)." I don't understand...


    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    
    int RandomNumber () //Computer selects a randomly a number within the interval
    {
        return rand() % 101 + 1;
    }
    
    int BinarySearch (int low, int high, int pick) // Algorithm function that finds the number
    {
        int guess = (high + low) / 2;
        int n = 0;
        while (guess != pick)
            {
                int guess = (high + low) / 2;
                if(guess < pick)
                {
                    low = guess + 1;
                    n = n + 1;
                }
                else if (guess > pick)
                {
                    high = guess - 1;
                    n= n + 1;
                }
                else if (guess == pick)
                {
                    n = n + 1;
                    return n;
                    break;
                }
            }
        if (guess == pick)
        {
            n = n + 1;
            return n;
        }
    }
    
    int main()
    {
        srand(time(NULL));
        int pick = RandomNumber();
        int tries = BinarySearch(0, 100, pick);
        cout << "For the randomly picked number" " " << pick << "in the [1, 100] interval, it took the binary search algorithm" " "<< tries << " " "tries. \n";
    }

  2. #2
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Are you sure you actually ran the program instead of just compiling it? The message you describe is what an IDE like Code:Blocks, for example, typically generates when you re-compile a program without making any changes in the source file. Because your program runs fine as it is.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    10
    I honestly don't know what happened I restarted codeblocks, pushed F9 as before and now it works... Thank you either way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ problem with binary search algorithm
    By aatrox in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2014, 08:21 AM
  2. Help with binary search with string algorithm
    By kabuki in forum C Programming
    Replies: 4
    Last Post: 10-03-2013, 01:11 PM
  3. binary search algorithm problem...
    By ssjnamek in forum C++ Programming
    Replies: 12
    Last Post: 09-29-2005, 03:28 PM
  4. Binary Search Tree Insertion Algorithm Help
    By bcianfrocca in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 07:35 PM
  5. Ornery binary search algorithm
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 03:32 PM

Tags for this Thread