i need some programming h elp

Discussion in 'OT Technology' started by notwist, Feb 27, 2005.

  1. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    I am supposed to make a program where the user selects a number between 1 and 100 and then the computer will attempt to guess that number. After each guess, the use will enter whether the number was too high or too low. So far i have this,

    function guessnum(low,high)

    n=floor((high-low)/2);
    disp(sprintf('%d',n));
    ans = input('High or Low? ','s')

    if strcmp(ans,'correct')
    disp(sprintf('Computer wins'));

    elseif strcmp(ans,'high')
    guessnum( ,n);

    elseif strcmp(ans,'low')
    guessnum(n, );


    end

    the thing i am having trouble with is what to put with each recursive function call. I know that if the number is too high, return that guess as the upper bound and vice versa with the lower bound. I just don't know how to calculate the lower bound if the guess is too high or the higher bound if the guess is too low. can OT help me out?:wavey:
     
  2. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    high and low - you want to reseed with the guess and one of the seeds.

    But wouldn't you want to reseed with guess+1 or guess-1 depending on whether it was to high or to low? And then when the upper and lower values become the same, well that's the number
     
  3. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
  4. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    guessnum(low,n-1) should be one of them. But to determine whether or not the computer is right, just compare high and low, if they are the same, then the computer knows the answer.
     

Share This Page