Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,456 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,916 people online right now. Registration is fast and FREE... Join Now!




computer guessing user number 1-100

 
Reply to this topicStart new topic

computer guessing user number 1-100

dodi
8 Aug, 2008 - 04:40 PM
Post #1

New D.I.C Head
*

Joined: 8 Aug, 2008
Posts: 1


need help to add or subtract 50% of guess after user responds h or l


CODE
#include <stdio.h>

int main(void)
{
int guess = 50;
int min_number = 1;
int max_number = 100;
char response;

printf("Pick an integer from 1 to 100. I will try to guess");
printf("it.\nRespond with h if my guess is high, with");
printf("\nan l if it is low and with c if correct.\n");
printf("Is your number %d?\n", guess);

while ((response = getchar()) != 'c') /*get response, compare to c */
    {
    if
     (response == 'l' && guess <= max_number)
       printf( "Well, then, is it %d?\n", ++guess*1.50);
      
    if
    (response == 'h' && guess >= min_number)  
       printf("Well, then, is it %d?\n", --guess);
            
    if
    (response == 'c')
     printf(" I knew I could do it!\n");
    
    
    else      
     printf("Sorry, I understand only l, h or c.\n");
     while (getchar() != '\n')
     continue;
     }    
return 0;
}

User is offlineProfile CardPM
+Quote Post

Mallstrop
RE: Computer Guessing User Number 1-100
8 Aug, 2008 - 04:44 PM
Post #2

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 32



Thanked: 1 times
My Contributions
The way to do it is the same as you see in a Binary search. (look that up)

You have a lower and an upper bound and each time the guess is the average of those numbers.

If the guess is too low, the lower bound becomes the middle value minus 1, otherwise the upper bound becomes the middle value plus one, each time cutting the potential answers in half.

This post has been edited by Mallstrop: 8 Aug, 2008 - 04:46 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 03:33PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month