Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am using an array to go through a file containing the numbers, 3, 77, 0, 29, 28 and I want to find the index FIRST multiple of 7, but my code finds all the mulitples of seven and outputs the index of 28 instead of 77.

 

Here is the coding I have so far:

 

 

Dim target() As Double

Dim upperbound As Integer

 

Form Load

Dim sr As IO.StreamReader = IO.File.OpenText("multiples.txt")

Dim change As Double

Dim i As Integer

 

upperbound = 0

Do While (sr.Peek <> -1)

change = sr.ReadLine

upperbound += 1

Loop

sr.Close()

 

ReDim target(upperbound)

sr = IO.File.OpenText("multiples.txt")

For i = 0 To upperbound - 1

target(i) = sr.ReadLine

Next

sr.Close()

End Sub

 

Private Sub btnDisplay_Click

Dim maxindx As Integer

lstBox.Items.Clear()

lstBox.Items.Add(find(maxindx))

End Sub

 

Function find(ByVal maxindx As Double) As Double

Dim i As Integer

Dim found As Double

 

maxindx = 0

For i = 0 To upperbound - 1

If target(i) Mod 7 = 0 Then

found = target(i)

maxindx = i

End If

Next i

Return maxindx

 

End Function

 

Thanks for helping!

  • Administrators
Posted

the for ... next loop will keep looping till all items in the array have been checked - you will need to either count backwards through the items or exit the loop when a match is found

For i = 0 To upperbound - 1
   If target(i) Mod 7 = 0 Then
       found = target(i)
       maxindx = i
       Exit For     'This should exit the loop after a match
   End If
Next i

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

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...