goddess_spanky
October 8th, 2001, 11:29 PM
This is a stupid beginner type question, but I need some help...
I have a multidimensional array StuStates(50,50) and I need to do a binary search on this array based on the user entering a two character state code (e.g. UT or CA). The first value in the array is the state code and the second value is the number of students in that state. what I am having problems with is performing the binary search...I know this is probably a really easy question, but I'm stumped. I have to do this for my VB class - otherwise I would never use this!
Here is the code so far:
option Explicit
option Base 1
Dim StuStates(50, 50) as string
private Sub cmdSearch_Click()
Dim i as Integer
Call Cls
for i = 1 to 50
print
next i
Call BinarySearch
End Sub
private Sub BinarySearch()
Dim Low as Integer, middle as Integer, high as Integer
Dim strSearchKey as string
Low = LBound(StuStates)
high = UBound(StuStates)
strSearchKey = txtSearch.Text
Do While (Low <= high)
middle = (Low + high) \ 2
Call PrintRow(Low, middle, high)
If (strSearchKey = StuStates(middle, middle)) then
txtResult.Text = "found " & strSearchKey & " in Index " & middle
Exit Sub
ElseIf strSearchKey < StuStates(middle, middle) then
high = middle - 1
else
Low = middle + 1
End If
Loop
print strSearchKey & " not found. "
End Sub
private Sub PrintRow(Low as Integer, middle as Integer, high as Integer)
Dim i as Integer
for i = LBound(StuStates) to UBound(StuStates)
If (i < Low Or i > high) then
print Space$(4);
ElseIf (i = middle) then
print StuStates(i, i) & vbTab
else
print StuStates(i, i) & vbTab
End If
next i
print
End Sub
private Sub Form_Load()
'Dim StuStates(50, 50) as string
StuStates(1, 1) = "AL" + "0"
StuStates(2, 2) = "AK" + "2"
StuStates(3, 3) = "AR" + "1"
StuStates(4, 4) = "AZ" + "5"
StuStates(5, 5) = "CA" + "12"
StuStates(6, 6) = "CO" + "7"
StuStates(7, 7) = "CT" + "0"
StuStates(8, 8) = "DE" + "0"
StuStates(9, 9) = "FL" + "2"
StuStates(10, 10) = "GA" + "1"
StuStates(11, 11) = "HA" + "3"
StuStates(12, 12) = "IA" + "0"
StuStates(13, 13) = "ID" + "13"
StuStates(14, 14) = "IL" + "0"
StuStates(15, 15) = "IN" + "0"
StuStates(16, 16) = "KS" + "1"
StuStates(17, 17) = "KY" + "2"
StuStates(18, 18) = "LA" + "0"
StuStates(19, 19) = "MA" + "0"
StuStates(20, 20) = "MD" + "0"
StuStates(21, 21) = "me" + "0"
StuStates(22, 22) = "MI" + "1"
StuStates(23, 23) = "MN" + "0"
StuStates(24, 24) = "MO" + "2"
StuStates(25, 25) = "MS" + "0"
StuStates(26, 26) = "MT" + "3"
StuStates(27, 27) = "NB" + "1"
StuStates(28, 28) = "NC" + "0"
StuStates(29, 29) = "ND" + "0"
StuStates(30, 30) = "NH" + "0"
StuStates(31, 31) = "NJ" + "0"
StuStates(32, 32) = "NM" + "4"
StuStates(33, 33) = "NV" + "0"
StuStates(34, 34) = "NY" + "1"
StuStates(35, 35) = "OH" + "0"
StuStates(36, 36) = "OK" + "0"
StuStates(37, 37) = "OR" + "2"
StuStates(38, 38) = "PA" + "0"
StuStates(39, 39) = "RI" + "0"
StuStates(40, 40) = "SC" + "0"
StuStates(41, 41) = "SD" + "0"
StuStates(42, 42) = "TN" + "0"
StuStates(43, 43) = "TX" + "2"
StuStates(44, 44) = "UT" + "55"
StuStates(45, 45) = "VA" + "0"
StuStates(46, 46) = "VT" + "0"
StuStates(47, 47) = "WA" + "3"
StuStates(48, 48) = "WI" + "0"
StuStates(49, 49) = "WV" + "0"
StuStates(50, 50) = "WY" + "6"
End Sub
~goddess
goddess_spanky@hotmail.com
I have a multidimensional array StuStates(50,50) and I need to do a binary search on this array based on the user entering a two character state code (e.g. UT or CA). The first value in the array is the state code and the second value is the number of students in that state. what I am having problems with is performing the binary search...I know this is probably a really easy question, but I'm stumped. I have to do this for my VB class - otherwise I would never use this!
Here is the code so far:
option Explicit
option Base 1
Dim StuStates(50, 50) as string
private Sub cmdSearch_Click()
Dim i as Integer
Call Cls
for i = 1 to 50
next i
Call BinarySearch
End Sub
private Sub BinarySearch()
Dim Low as Integer, middle as Integer, high as Integer
Dim strSearchKey as string
Low = LBound(StuStates)
high = UBound(StuStates)
strSearchKey = txtSearch.Text
Do While (Low <= high)
middle = (Low + high) \ 2
Call PrintRow(Low, middle, high)
If (strSearchKey = StuStates(middle, middle)) then
txtResult.Text = "found " & strSearchKey & " in Index " & middle
Exit Sub
ElseIf strSearchKey < StuStates(middle, middle) then
high = middle - 1
else
Low = middle + 1
End If
Loop
print strSearchKey & " not found. "
End Sub
private Sub PrintRow(Low as Integer, middle as Integer, high as Integer)
Dim i as Integer
for i = LBound(StuStates) to UBound(StuStates)
If (i < Low Or i > high) then
print Space$(4);
ElseIf (i = middle) then
print StuStates(i, i) & vbTab
else
print StuStates(i, i) & vbTab
End If
next i
End Sub
private Sub Form_Load()
'Dim StuStates(50, 50) as string
StuStates(1, 1) = "AL" + "0"
StuStates(2, 2) = "AK" + "2"
StuStates(3, 3) = "AR" + "1"
StuStates(4, 4) = "AZ" + "5"
StuStates(5, 5) = "CA" + "12"
StuStates(6, 6) = "CO" + "7"
StuStates(7, 7) = "CT" + "0"
StuStates(8, 8) = "DE" + "0"
StuStates(9, 9) = "FL" + "2"
StuStates(10, 10) = "GA" + "1"
StuStates(11, 11) = "HA" + "3"
StuStates(12, 12) = "IA" + "0"
StuStates(13, 13) = "ID" + "13"
StuStates(14, 14) = "IL" + "0"
StuStates(15, 15) = "IN" + "0"
StuStates(16, 16) = "KS" + "1"
StuStates(17, 17) = "KY" + "2"
StuStates(18, 18) = "LA" + "0"
StuStates(19, 19) = "MA" + "0"
StuStates(20, 20) = "MD" + "0"
StuStates(21, 21) = "me" + "0"
StuStates(22, 22) = "MI" + "1"
StuStates(23, 23) = "MN" + "0"
StuStates(24, 24) = "MO" + "2"
StuStates(25, 25) = "MS" + "0"
StuStates(26, 26) = "MT" + "3"
StuStates(27, 27) = "NB" + "1"
StuStates(28, 28) = "NC" + "0"
StuStates(29, 29) = "ND" + "0"
StuStates(30, 30) = "NH" + "0"
StuStates(31, 31) = "NJ" + "0"
StuStates(32, 32) = "NM" + "4"
StuStates(33, 33) = "NV" + "0"
StuStates(34, 34) = "NY" + "1"
StuStates(35, 35) = "OH" + "0"
StuStates(36, 36) = "OK" + "0"
StuStates(37, 37) = "OR" + "2"
StuStates(38, 38) = "PA" + "0"
StuStates(39, 39) = "RI" + "0"
StuStates(40, 40) = "SC" + "0"
StuStates(41, 41) = "SD" + "0"
StuStates(42, 42) = "TN" + "0"
StuStates(43, 43) = "TX" + "2"
StuStates(44, 44) = "UT" + "55"
StuStates(45, 45) = "VA" + "0"
StuStates(46, 46) = "VT" + "0"
StuStates(47, 47) = "WA" + "3"
StuStates(48, 48) = "WI" + "0"
StuStates(49, 49) = "WV" + "0"
StuStates(50, 50) = "WY" + "6"
End Sub
~goddess
goddess_spanky@hotmail.com