Thread: Binary Search Issues!

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    1

    Binary Search Issues!

    Hey guys, I'm just a newbie code writer..
    I'm playing with a guessing game program as a personal exercise, but I'm missing a vital piece - the binary search-style code. I was wondering if you guys could help me solve this!

    "Have the program initially guess 50, and have it ask the user whether the guess is high, low, or correct. If, say, the guess is low, have the next guess be halfway between 50 and 100, that is, 75. If that guess is high, let the next guess be halfway between 75 and 50, and so on." (We're assuming that the user won't cheat.)

    I need the average, essentially. As in, (50 + 75) / 2 = 63.. but when I use this method of "guess = (high+low)/2, it just keeps giving me 50.
    I can't remember what operators I should use to increment the program's response based on the user's input, and can't find help anywhere - which is making me crazy for a solution. It's literally a binary search, that needs to go where those TODOs are. If low was chosen, it would have to start by being at least 51, to 100, so I'd have to set that, then find the average.

    Does anyone know of an efficient way to set this up? If I don't find an answer I'll drive myself crazy haha

    Code:
    #include <stdio.h>
    Code:
    #include <ctype.h>
    
    int main(int argc, const char * argv[])
    {
    int low;
    int high;
    int guess;
    int response;
    int toupper ( int );
    
    do
    {
        low = 1;
        high = 100;
        guess = 50;
    
        printf("Pick an integer from 1 to 100 and I will try to guess it.\n");
        printf("If I guess too low, respond with a L.\n");
        printf("If I guess too high, respond with a H.\n");
        printf("If I guess correctly, respond with a Y.\n");
        printf("Okay, is your number %d?\n",guess);
    
        while( (response=getchar()) != 'y')
        {
            if( response == 'l' )
            {
                // TODO: Ajust the value of low
                // TODO: Ajust the value of guess
                printf("Too low eh...well then, is it %d?\n",guess);
            }
            else if( response == 'h' )
            {
                // TODO: Ajust the value of high
                // TODO: Ajust the value of guess
                printf("Too high eh...well then, is it %d?\n",guess);
            }
            else if( response != '\n' )
            {
                fflush(stdin);
                printf("Sorry, I only understand L, H and Y. Please try again.\n");
            }
        }
    
        printf("Please enter R to restart, or Q to quit.\n");
        response=getchar();
    
        while ((response != 'q') && (response != 'r'))
        {
            if (response != '\n')
            {
                fflush(stdin);
                printf("Oops!  Please enter R or Q.\n");
            }
            response=toupper(getchar());
        } 
    } while (response=='R'); 
    
    return 0; }
    
    



    Last edited by pixelpinch; 04-05-2014 at 01:16 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Also posted:
    syntax error - Responses in C - Guessing Game - Stack Overflow
    Incrementing Responses in C - Binary Search - Stack Overflow

    Cross posting (posting to multiple forums) is considered bad etiquette. Read this link, it will make your forum experience much more useful. You should also try waiting more than an hour or two before you make a new thread or give up and run to another forum.

    Where ever you got the code from, it's crap and parts of it don't work (e.g. user input). I would ditch it and start from scratch. Write it all yourself -- it's good practice.

    fflush(stdin) results in undefined behavior. See this link. Of course, you've been told that multiple times on Stack Overflow, but you just don't seem to want to take the advice you're given; even whe it could, in theory, be the entire cause of your problem (undefined behavior is undefined, so anything can happen).

    The few lines of code you need are very basic. I suggest you re-read your textbook and class notes, and some online tutorials. Work through all the examples, from the very first page. That will give you the fundamentals you need to complete this program.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by pixelpinch View Post
    I need the average, essentially. As in, (50 + 75) / 2 = 63.. but when I use this method of "guess = (high+low)/2, it just keeps giving me 50.
    Nothing in the code you supplied does that calculation.

    Quote Originally Posted by pixelpinch View Post
    Does anyone know of an efficient way to set this up?
    That depends what you mean by "efficient". There are many measures of efficiency.

    For a measure of efficiency based on "your effort to set it up", I suggest most efficient way would be for you to actually code something that does the required calculation. You have already expended more effort in creating this post (and cross-posting) than would be needed to set it up yourself.

    Going forward, the effort to convince someone else to do it for you (i.e. to beg in forums for a solution) would also exceed the effort of doing it yourself.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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