codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Finding the smallest number in a set of integers.  deedlit at 00:21 on Wednesday, November 19, 2003
 

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

[/CODE]

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!

  Re: Finding the smallest number in a set of integers.  balabaster at 21:03 on Thursday, December 11, 2003
 

I'd read the file into an array and then use a min or max function something like this:

Sub Main()

Dim oFileSys As Scripting.FileSystemObject
Dim oFile As File
Dim oTxtStrm As TextStream
Dim NumSet() As Integer
Dim NumCount As Integer

Set oFileSys = CreateObject("Scripting.FileSystemObject")
Set oFile = oFileSys.GetFile("/forum/DNumberList.txt")
Set oTxtStrm = oFile.OpenAsTextStream(ForReading)

NumCount = 0

Do While Not oTxtStrm.AtEndOfStream
NumCount = NumCount + 1
'Expand Array
ReDim Preserve NumCount(NumCount) As Integer
NumSet(NumCount) = Val(oTxtStrm.ReadLine)
Loop

MinNum = Min(NumSet)
MaxNum = Max(NumSet)

oTxtStrm.Close
Set oTxtStrm = Nothing
Set oFile = Nothing
Set oFileSys = Nothing

End Sub

Public Function Min(ParamArray NumberSet() As Integer) As Integer
'Return the minimum integer from a set of integers

Dim i As Integer
Dim CurrentMin As Integer

'Set CurrentMin to the first number in the set
CurrentMin = LBound(NumberSet)

'Run a loop from the 2nd number in the set to the end of the set
For i = LBound(NumberSet) + 1 To UBound(NumberSet)

'If the current number in the set is less than the one in CurrentMin,
'then we have a new current minimum, store the current number in current min
If NumberSet(i) < CurrentMin Then CurrentMin = NumberSet(i)

Next i

Min = CurrentMin

End Function

Public Function Max(ParamArray NumberSet() As Integer) As Integer
'Return Maximum integer from a set of numbers

Dim i As Integer
Dim CurrentMax As Integer

'Set CurrentMax to the first number in the set
CurrentMax = LBound(NumberSet)

'Run a loop from the second number in the set to the end of the set
For i = LBound(NumberSet) + 1 To UBound(NumberSet)

'If the current number in the set is greater than CurrentMax,
'Set CurrentMax to the current number in the set
If NumberSet(i) > CurrentMax Then CurrentMax = NumberSet(i)

Next i

Max = CurrentMax

End Function








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: TO SEND Text message or URL through Clickatell
•  A little help
•  Re: Storing data from HTML to Excel or TXT
•  Re: Parsing, fish up all between <td> and </td> ... without possibility of confusion.
•  Technical problem with Visual Studio C++
•  Put confirm do you really on button instead of link
•  How Can i Create a list[Validation] in embedded Excel Object in JavaScript ?
•  Re: searching for gd::graph guide
•  Re: hash within hash


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008