Project i'm struggling on

i have a text file named “Words.text”

and i want the program to do this:
[b]Read the words into an array

allow the user to enter a word

Look for the word using the binary search

Display the cell number where the word was found or that the word doesn’t exist in the list.[/b]

Maybe show what you have tried and what hasn’t worked? You shouldn’t expect someone to make the project for you.

in a module

dim d as dictionary
SUB ReadData
dim i as integer
dim v() as string
dim t as textinputstream
dim f as folderitem
d=new dictionary
f=specialfolder.desktop.child("words.text") // assumes file is on desktop
t=textinputstream.open(f)
s=t.readall
t.close
v=split(s," ") // assuming words are space delimited
for i=0 to v.ubound
    addWord(v(i))
next i
END SUB
SUB AddWord(word as string)
   if not d.haskey(trim(word)) then d.value(word)=true
END SUB

This was typed off the top of my head… and may contain errors…

But no Binary Search is required… Dictionary uses very fast hash look up

AddWord is used to read the original list, as well as process user input

[quote=165143:@Austin Higgins]i have a text file named “Words.text”

and i want the program to do this:
[b]Read the words into an array

allow the user to enter a word

Look for the word using the binary search

Display the cell number where the word was found or that the word doesn’t exist in the list.[/b][/quote]
Please post what code you have been trying with??

Or a link to the homework page…? :slight_smile:

I have a feeling that binary search IS required, and that this is the likely point of the task.
I note the question also says ‘display in the cell’
So although the question asks to read into an array, it probably more relates to displaying in a listbox or Excel

So, Dave’s code to read a file from disc and turn it into a dictionary of words is valid.
The binary search requires you to sort the list, then start half way through it and see if thats the word.
If not, divide the rest of the list above or below in half and try the half way word again, until you find the one you want.

Also important: how many words? 1.000 is simple and 1.000.000 is more fun.

@Mike Cotrone @Dave S
Dim top as integer
Dim middle as integer
Dim bottom as integer
Dim theword as string
Dim j as integer
Dim F as FolderItem
F = GetFolderItem(“words.txt”)
textfield1.text = theword
top = 0
bottom = words.ubound
middle = ((top+bottom)/2)
Do until theword = Words(middle)
if theword < str(middle) then
top = middle + 1
end if
if theword > str(bottom) then
bottom = middle - 1
end if
loop
if textfield1.enabled = true then
msgbox “the word was found”
end if
if textfield1.enabled = false then
msgbox “the word was not found”
end if

This closes my program after i run it

You may want to start with the intro to programming book.
http://www.xojo.com/learn/