Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » General

Post new topic   Reply to topic
Finding Min in collection of numbers
View previous topic :: View next topic  
Author Message
deedlit
Newbie


Joined: 18 Nov 2003
Posts: 1

PostPosted: Nov 18th, 2003 07:19 PM    Post subject: Finding Min in collection of numbers Reply with quote

Hi! I am having trouble with one little part of my program

I have to find the min in a collection of data that's already been placed in a text file in the bin folder.

I know that if I were finding the largest, I could set Dim largest = 0, but this does not work for min. We cannot use negative numbers so the smallest would end up always being zero.
I need to know how to get around that.

Code:

Private Sub btnFindSmallest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindSmallest.Click
        'Finds smallest of a collection of numbers.
        Dim smallest, num As Double
        smallest = 999
        Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")
        Do While sr.Peek <> -1
            num = CDbl(sr.ReadLine)
            If num < smallest Then
                smallest = num
            End If
        Loop
    End Sub



where I have smallest = 999, I need it to be able to not just check up to 999 but to infinity if necessary because the smallest will always be zero if I set it equal to zero.
Back to top
View user's profile Send private message
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Nov 19th, 2003 07:13 AM    Post subject: Reply with quote

Set it equal to the first number and then compare it with the rest. If the number you have now is bigger than the one you are comparing it to then set the new one as min, if not then check the next Wink
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » General All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic