Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Help with Recurvise Function in VB 2005

Name: Anonymous 2007-05-03 4:18 ID:0u2GQLpA

Hay guys... Need some help with some programming in Visual Basic. I know, I know, VB is for newbs and tards... I don't make the courses my university offers, sorry. In any case, I've never been great with recursion, and this is one of those... I need to replace the iterative function BinarySearch with it's recursive equivalent. Code is as follows:
---------------------------------------------

   Dim array1 As Integer() = New Integer(14) {}

   ' FrmBinarySearch initializes array1 to ascending values
   ' 0, 2, 4, 6, ..., 28 when first loaded
   Private Sub FrmBinarySearch_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load

      Dim i As Integer

      For i = 0 To array1.GetUpperBound(0)
         array1(i) = 2 * i
      Next

   End Sub ' FrmBinarySearch_Load

   ' event handler for cmdFindKey button
   Private Sub cmdFindKey_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles cmdFindKey.Click

      Dim searchKey As Integer = Convert.ToInt32(txtInput.Text)

      lblDisplay.Text = ""

      ' perform binary search
      Dim element As Integer = BinarySearch(array1, searchKey)

      If element <> -1 Then
         lblResultOutput.Text = "Found value in element " & element
      Else
         lblResultOutput.Text = "Value not found"
      End If

   End Sub ' cmdFindKey_Click

   ' performs binary search
   Function BinarySearch(ByVal array As Integer(), _
      ByVal key As Integer) As Integer

      Dim low As Integer = 0                 ' low index
      Dim high As Integer = array.GetUpperBound(0) ' high index
      Dim middle As Integer             ' middle index

      While low <= high
         middle = (low + high) \ 2

         ' the following line displays part
         ' of the array being manipulated during
         ' each iteration of loop
         BuildOutput(low, middle, high)

         If key = array(middle) Then     ' match
            Return middle
         ElseIf key < array(middle) Then ' search low end
            high = middle - 1            ' of array
         Else
            low = middle + 1
         End If

      End While

      Return -1 ' search key not found
   End Function ' BinarySearch

   Sub BuildOutput(ByVal low As Integer, _
      ByVal middle As Integer, ByVal high As Integer)

      Dim i As Integer

      For i = 0 To array1.GetUpperBound(0)

         If i < low OrElse i > high Then
            lblDisplay.Text &= "    "
         ElseIf i = middle Then  ' mark middle element in output
            lblDisplay.Text &= String.Format("{0:D2}", _
               array1(i)) & "* "
         Else
            lblDisplay.Text &= String.Format("{0:D2}", _
               array1(i)) & "  "
         End If

      Next i

      lblDisplay.Text &= vbCrLf
   End Sub ' BuildOutput

End Class ' FrmBinarySearch

Name: Anonymous 2007-05-03 4:43 ID:mlM72AAG

Hay guys... Need some help with some programming in Visual Basic. I know, I know, VB is for newbs and tards... I don't make the courses my university offers, sorry. In any case, I've never been great with recursion, and this is one of those... I need to replace the iterative function BinarySearch with it's recursive equivalent. Code is as follows:
---------------------------------------------
10 PRINT "LOL"
20 GOTO 10

Name: Anonymous 2007-05-03 5:35 ID:0u2GQLpA

Nice. Fag. Anyone with any serious help?

Name: Anonymous 2007-05-03 5:38 ID:W1JZG+CM

well, what the fuck does it do?

Name: Anonymous 2007-05-03 6:11 ID:0LCDRhyY

DECLARE SUB LOL ()
SUB LOL ()
  PRINT "LOL"
  LOL
END SUB
LOL

Name: Anonymous 2007-05-03 6:26 ID:mE3DrS4Y

>>3
On 4chan?

Name: Anonymous 2007-05-03 7:08 ID:mlM72AAG

>>3
Sir, You dont want to talk to me like that. Ive read SICP after all.

Name: Anonymous 2007-05-03 7:43 ID:JVMpvMOc


         ;x86 ASSEMBLY
         ;RECURSION EXAMPLE

RECURSE  CALL RECURSE         ;CALL RECURSION SUBROUTINE

Name: Anonymous 2007-05-03 8:28 ID:6voECMnh


Try this:

Function BinarySearch(
    ByVal array As Integer(), _
    ByVal key As Integer, _
    ByVal low as Integer, _
    ByVal high as Integer) As Integer

    Dim middle As Integer             ' middle index

    If low <= high Then
        middle = (low + high) \ 2

        ' the following line displays part
        ' of the array being manipulated during
        ' each iteration of loop
        BuildOutput(low, middle, high)

        If key = array(middle) Then     ' match
            Return middle
        Else
            If key < array(middle) Then ' search low end
                high = middle - 1            ' of array
            Else
                low = middle + 1
            End If
            Return BinarySearch(array, key, low, high)
        End If

    Else
        Return -1 ' search key not found
    End If

End Function


And then call it with BinarySearch(array1, SearchKey, 0, array1.GetUpperBound(0)) in your cmdFindKey_Click function

Name: Anonymous 2007-05-03 10:46 ID:Heaven

sage for vb

Name: Anonymous 2007-05-03 10:57 ID:KNojlCAq

>>1
I've never been great with recursion
OMG! THE HERESY!

Go read SICP. Now.

Name: Anonymous 2007-05-03 11:41 ID:Li02yfu1

>>11
More like "go read chapter 1 of SICP and be great with recursion in under a week".

Name: Anonymous 2007-05-03 12:39 ID:mlM72AAG

>>12
is that even relevant to the topic or is this just your way of saying "ive read SICP"?

Name: Anonymous 2007-05-03 17:16 ID:d32wxv0f

>>12
more like "go read the first paragraph of that chapter from SICP and write a program that simulates the human brain within 256 seconds"

Name: Anonymous 2007-05-03 17:26 ID:mlM72AAG

>>14
is that even relevant to the topic or is this just your way of saying "ive read SICP"?

Name: Anonymous 2009-01-14 15:10

WTF

Name: HAXUS THE SAGE 2011-10-07 0:53

Not /prog/ related. GTFO!

Name: Anonymous 2011-10-07 0:58

int a += a++;

Name: Anonymous 2011-10-07 2:34

>>18
Not valid VB.net.
What's that between your cheeks? Rice-A-Roni?!?! Therefore, you're absolutely worthless garbage; you might as well just turn to dust and die!

Name: Anonymous 2011-10-07 3:06

Something like this I guess
Function BinarySearch(ByVal Data As Integer(),ByVal Key As Integer,ByVal Position As Integer) As Integer
    Dim NextPosition As Integer
    If Data(Position) < Key
        NextPosition = Position \ 2
    ElseIf Data(Position) > Key
        NextPosition = CInt(System.Math.Ceiling((Position + Data.Length) / 2))
    Else
        Return Position
    End If
    If NextPosition = Position Or NextPosition < 0 Or NextPosition >= Data.Length Then
        Return -1
    Else
        Return BinarySearch(Data,Key,NextPosition)
    End If
End Function

Name: Anonymous 2011-10-07 3:14

>>20
Function ByVal As Integer(),ByVal As Integer,ByVal As Integer) As Integer
So, where is the code? I see only some abstract Set Theory bullshit. And why do they adversites a math intensive language (Basic) as good language for newbiews? Do newbiews care about "the Set of all integers" or "domain of a function"? Or maybe they care about covariance and contrvariance?

Name: Anonymous 2011-10-07 3:15

>>21
newbies
self fix

Name: Anonymous 2011-10-07 5:51

>>21
The code is inside of you are ass.

Name: Anonymous 2011-10-07 10:27

http://reprog.wordpress.com/2010/04/19/are-you-one-of-the-10-percent/

Has anyone found the bug, or is OP miraculously correct?

Name: Anonymous 2011-10-07 12:34

>>24
OP auto-fails for the use of VB even if the code is correct.

Name: Anonymous 2011-10-07 13:01

Fuck VB is ugly.

Name: Sgt.Kabu洹儭kimanḧ 2012-05-29 1:16

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List