Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Recursive Binary search...
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Lapsus Antepedis




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
goboenomo




PostPosted: Thu Oct 13, 2005 11:36 am   Post subject: (No subject)

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




PostPosted: Thu Oct 13, 2005 3:07 pm   Post subject: (No subject)

No. I suggest you look up the term.

*edit* Woah! 1001'th post!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: