|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
hi guys, I've implemented this flawed code.
For a hangman game The users 'guess' and the word to guess is passed to the binary Search, the code is then supposed to search for the guess in theWord to tell the user wether the guess they have chosen is present. Code:
procedure binSearch(theWord : string; n : integer; guess : char);
var
left, mid, right : integer;
begin
left := 0;
right := n-1;
while (left <= right) do
begin
mid := (left + right) div 2;
if guess > theWord[mid] then
left := mid + 1
else if guess < theWord[mid] then
right := mid - 1
else
writeln('Found!');
left := right + 1//creates a condition to terminate loop
end;
end;
procedure monkey();
var
cat : myArray;
choice, n : integer;
theWord : string;
guess : char;
begin
choice := 1;
loadCat(cat); //loads the catergory
getEntry(cat, choice, theWord); //choses an entry from the category
n := length(theWord); //gets the length of theWord and sends it to
the sort.
sort(theWord, n);
write('Take a guess!: ');
readln(guess);
binSearch(theWord, n, guess);
end;
Help with this would be really really appreciated! Many thanks in advance. |
|
#2
|
||||
|
||||
|
So we're supposed to look at your wrong code, know what it's supposed to do and fix it so it does what you intended? That's all the info?
Hmmm ....
__________________
medialint.com "Energy has the opportunity to change the climate if it's done right." - Sen. John Ensign, R-Nev. (quoted out of context) |
|
#3
|
|||
|
|||
|
Quote:
Lol, sorry added some info at the top. Thanks for the reply. |
|
#4
|
||||
|
||||
|
Setting aside the errors in your implementation of the algorithm, I don't think that a binary search is going to work for this type of problem without more work. Binary chop only works if the data being searched through is sorted. For example, if you have a word 'supercilious' as the target the player is trying to guess, and the player guesses 'r', then a binary search through the word would go like this:
Thus, to use a binary chop for this purpose, you would have to have a sorted copy of the word first:
Note that you would want to have the sorted copy separate from the actual word, otherwise you would not be able to display it later. You will also need to remove or mark the letters which have been already found, since there may be more than one instance of them in the word. To be honest, given the size of the searched data, a binary search is probably overkill, especially given that you need to sort it first; chances are, anything less than 100+ letters will be faster with a brute-force linear search. You'd have to check that yourself, but either way the difference is probably so small as to be effectively unmeasurable.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF #define KINSEY (rand() % 7) λ Scheme is the Red Pill Scheme in Short • Understanding the C/C++ Preprocessor Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1 FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov Last edited by Schol-R-LEA : January 8th, 2008 at 10:55 PM. |
|
#5
|
|||
|
|||
|
Quote:
Thanks for your reply. This 'issue' has now been resolved. You are correct, binary Search is overkill for what I want, I just had an opportunity to do (because part of this program requires a search) so i thought i'd give it a try. I did sort the string into characters of ascending alphabetical order already, but nm.When I posted this original thread last night I was very tired and exhausted and my brain had sizzled (meltdown was greater than it is now!) I left many details out of the background to do with the code. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > BinarySearch is kickng my *** |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|