Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

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.

 

Thanks.

  • Leaders
Posted

Who is 'we'? :)

Does your code not work?

What if you set smallest = PositiveInfinity ?

Does it work then?

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • *Experts*
Posted

Set it to Double.MaxValue - that's the highest value a Double can hold, approximately 1.79769313486232e308.

 

[edit]Heh, d'oh - I should've realized Iceplug was going to reply to this thread. Got to it through Who's Online.[/edit]

Posted

You dont have to pre set the smallest variable.

 

Would it not be better to make the smallest equal to the first line, then input the rest and compair, this will work no matter what variable type you use!

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
       Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")
       if sr.peak <> -1 then
       smallest = cdbl(sr.readline)
       Do While sr.Peek <> -1
           num = CDbl(sr.ReadLine)
           If num < smallest Then
               smallest = num
           End If
       Loop
       end if
   End Sub

I think that should do it...

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