Computer Science Canada

Recursive Binary search...

Author:  Lapsus Antepedis [ Tue Oct 04, 2005 9:27 am ]
Post subject:  Recursive Binary search...

I posted a binary search string guesser somewhere here a whie ago, but I can't find the old topic... Anyway, one of the posts in the topic was a suggestion that I try the same thing, but with recursion. I finally got around to doing it, so here's the new code...

code:
var step : int := 0

fcn stringguess (mini, maxi, letter : int, line : string) : string

    step += 1
    delay (20)

    if letter <= length (line) then
        var guess : int := mini + (abs (mini - maxi) div 2)
        locate (2, letter)
        if chr (guess) > line (letter) then
            put chr (guess)
            result stringguess (mini, guess, letter, line)
        elsif chr (guess) < line (letter) then
            put chr (guess)
            result stringguess (guess, maxi, letter, line)
        elsif chr (guess) = line (letter) then
            put chr (guess)
            locate (2, letter + 1)
            result stringguess (0, 255, letter + 1, line)
        end if
    else
        result " "
    end if

end stringguess

var line : string

loop

    step := 0
    put "Please enter a string of characters: " ..
    get line : *
    cls
    put line
    put stringguess (0, 255, 1, line)
    put step - 1

end loop


The function itself is only 19 lines, excluding the delay and step counter that make it work exactly like the other program...
How did I do for my second try at recursion?

EDIT: Added loop to make program function Exactly like the other.

Author:  goboenomo [ Thu Oct 13, 2005 11:36 am ]
Post subject: 

if this is supposed to be a binary digit finder... then it does not work

Author:  [Gandalf] [ Thu Oct 13, 2005 3:07 pm ]
Post subject: 

No. I suggest you look up the term.

*edit* Woah! 1001'th post!


: