Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I`ve got a function searching for a spesific month in an array. The month is part of a full date like this:(dd.mm.yyyy) I`ve got a text box, where I write a number representing a month, which is the input of the function which returns the position in the array for that specific month. The sorted array has been filled from "txtPerson.text" looking somthing like this:

 

Name

date

address

Phone

 

i.e

 

Adolf

14.02.1980

Duckstreet 5, ohio

555-89898

 

It seems to me that the function does not return the position in the array, Why not, what am I doing wrong?


' Ignore the fact that Month is a reserved word.

Structure individual
       Dim name As String
       Dim dates As String
       Dim address As String
       Dim Phone As String
   End Structure

   Dim person(8) As individual
.
.
Private Sub Button1_Click.....
      Dim Temp as Integer
      Temp =  MonthSearch(cInt(txtNumber.text))
End Sub
.
.
.
Function MonthSearch(ByVal month As Integer) As Integer
       Dim first, middle, last As Integer
       first = 1
       last = numPerson
       Do While (first <= last)
           middle = CInt((first + last) / 2)
       Select Case CInt(CDate(person(middle).dates).Month)
               Case Month
                   Return middle
               Case Is > Month
                   last = middle - 1
               Case Is < Month
                   first = middle + 1
               Case Else
           End Select
       Loop
       Return 0
   End Function

Posted

You are using Binary search to find the month, so your persons should be in ascending order of month in the array!!

 

see the code below

' Ignore the fact that Month is a reserved word.

Structure individual

Dim Name As String

Dim dates As String

Dim address As String

Dim Phone As String

End Structure

 

Dim person(8) As individual

 

Private Sub SearchIndividual()

Dim Temp As Integer

Temp = MonthSearch(CInt(txtNumber.Text))

 

If Temp > -1 Then

MsgBox(person(Temp).Name & vbLf & person(Temp).dates)

Else

MsgBox("Not Found")

End If

 

End Sub

 

Function MonthSearch(ByVal month As Integer) As Integer

Dim first, middle, last As Integer

first = 1

last = 8

Do While (first <= last)

middle = CInt((first + last) / 2)

Select Case CInt(CDate(person(middle).dates).Month)

Case month

Return middle

Case Is > month

last = middle - 1

Case Is < month

first = middle + 1

Case Else

End Select

Loop

Return -1

End Function

 

Private Sub FillArray()

Dim snIndex As Integer

 

For snIndex = 1 To 8

person(snIndex).Name = "Person" & snIndex

person(snIndex).address = "Address" & snIndex

person(snIndex).Phone = "Phone" & snIndex

person(snIndex).dates = "01.0" & snIndex & ".2002"

Next

 

End Sub

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...