Thread: Can someone please help with this guessing program?

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    5

    Lightbulb Can someone please help with this guessing program?

    Hey, all. I'm supposed to be writing a guessing program where the computer is the one guessing the number. My objective is to modify it to the point where if, say, the initial guess is 50, and if it is too low, then have the next guess be between 50 and 100, which would be 75. If the guess is too high, then have it be between 75 and 50.

    Here's my code so far:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void betterguess(void)
    {
        char response;
        int high = 100;
        int low = 0;
        int guess = 50;
        
        printf("I'm going to try and guess a number from 1 to 100. If I guess your "
                "number, then press y. \n");
        printf("Is your number %d?\n", guess);
        printf("If the number needs to be higher, then press 1.\n");
        printf("If the number is lower, then press 2.\n");
        
        while ((response = (char)tolower(getchar())) != 'y')
        {
        while (getchar() != '\n')
            ;
    
            if (response == '1')
            {    guess = high + (high + low) / 2;
            printf("Well, is your number %d?\n", guess++);
            }
            if (response == '2')
            {    guess = low = (high + low) / 2;
            printf("Well, is your number %d?\n", guess--);
            }
        }
        printf("I knew I could do it!\n");
    }
    After fidgeting around with the code, and using if statements to create a higher and lower design, I can't quite get it down. Here's the I/O.

    I'm going to try and guess a number from 1 to 100. If I guess your number, then press y.
    Is your number 50?
    If the number needs to be higher, then press 1.
    If the number is lower, then press 2.
    1
    Well, is your number 150?
    2
    Well, is your number 50?
    2
    Well, is your number 75?
    2
    Can someone help me?

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    so it whittles it down by one each guess? because you have
    printf("Well, is your number %d?\n", guess--);
    or it adds to it by 1 because you have
    printf("Well, is your number %d?\n", guess++);


    or is it suppose to devide it in half each time because you have

    guess = low = (high + low) / 2;
    guess = high + (high + low) / 2;

    50 % each way
    Last edited by userxbw; 10-19-2017 at 03:58 PM.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    take a look at this. is that even close to what you need?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void betterguess(void)
    {
        char response;
        int high = 100;
        int low = 0;
        int guess = 50;
    
        printf("I'm going to try and guess a number from 1 to 100. If I guess your "
                "number, then press y. \n");
        printf("Is your number %d?\n", guess);
        printf("If the number needs to be higher, then press 1.\n");
        printf("If the number is lower, then press 2.\n");
    
        while ((response = (char)tolower(getchar())) != 'y')
        {
        while (getchar() != '\n')
            ;
    
            if (response == '1')
            {  // guess = high + (high + low) / 2;
               guess = guess + (high + low) / 2;
            printf("Well, is that your number %d?\n", guess);
          //  printf("Well, is your number %d?\n", (high + (high + low) / 2 ));
            }
            if (response == '2')
            {  //  guess = low = (high + low) / 2;
              guess = guess - (high + low) / 2;
            printf("Well, is that your number %d?\n", guess);
            //printf("Well, is your number %d?\n", (low = (high + low) / 2 ) );
            }
        }
        printf("I knew I could do it!\n");
    }
    
    
    int main (void)
    {
    
    betterguess();
    
    return 0;
    }
    Last edited by userxbw; 10-19-2017 at 03:54 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The only problem that I can immediately see is the math.

    The fist guess needs to be the median number.
    guess = (hi - low) / 2 + low;

    If the number is higher, you can do this again in the 50 to 100 range.
    guess = (hi - guess) / 2 + guess;

    If the number is lower, there is a similar calculation, except guess is the high end.

    Once you bisect the number line enough, you will get the guess right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do i program a guessing game?
    By Gayathri in forum Windows Programming
    Replies: 3
    Last Post: 03-28-2012, 11:46 AM
  2. how do i program a guessing game?
    By Silsilay in forum General AI Programming
    Replies: 3
    Last Post: 03-28-2012, 10:46 AM
  3. Need help with letter guessing program
    By ltdec in forum C Programming
    Replies: 31
    Last Post: 10-05-2011, 12:29 AM
  4. Guessing program Hm. Help
    By snugy in forum C Programming
    Replies: 3
    Last Post: 11-17-2008, 11:09 AM
  5. Guessing Program
    By webren in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2005, 07:24 PM

Tags for this Thread