mary13 Posted November 18, 2003 Posted November 18, 2003 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! Quote
Administrators PlausiblyDamp Posted November 18, 2003 Administrators Posted November 18, 2003 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mary13 Posted November 18, 2003 Author Posted November 18, 2003 Thanks! Thanks for helping find the first element in an array, I got it working. Thanks again! Mary Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.