Solved

find maximum and minimum from text file

Posted on 2006-07-16
10
330 Views
Last Modified: 2010-05-18
I have a text file with 2 column and about 2000 rows of value. It looks like below

709.0 1220.0
391.0 1181.0
295.0 303.0
289.0 1303.0
2714.0 303.0
2745.0 1303.0
261.0 1254.0
And so on…

I want to write a program to find the max value for column 1 and minimun value for column 2. please help me to do it. thanks in advance.
0
Comment
Question by:rmtogether
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 150 total points
ID: 17119123
Dim fileName as string
Dim fileNo as integer

Dim maxCol1 as double
Dim minCol2 as double
Dim firstRow as boolean

Dim varString as string
Dim varArray as variant

fileName = "C:\temp\fileName.txt"
fileNo = freefile
open fileName for input as #fileNo

firstRow = false

Do while not eof(fileno)
      line input #fileNo, varString
   varArray = split(varString, " ")
      if (firstRow = false) then
            maxCol1 = varArray(0)
      minCol2 = varArray(1)
            firstRow = true
      else
            if (varArray(0) > maxCol1) then
                  maxCol1 = varArray(0)
            end if

            if (varArray(1) < minCol2) then
                  minCol2 = varArray(2)
            end if
      end if
loop

Debug.print "Maximum Value From Col1 is " & maxCol1
Debug.print "Minimum Value From Col2 is " & minCol2
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17119126
You can do this with file system objects - but this works just as well,

It opens the file, splits the results into an array (using " " as the delimiter)

The first loop it puts the initial values into the max and min values.

Each subsequent loop it compares the previous max and min values with the current row and changes if they are greater / less
0
 

Author Comment

by:rmtogether
ID: 17119248
thank you!!

but,
I have complie problem in the line minCol2 = varArray(2). the error message is "subscript out of range" could you please help me debug this problem
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 6

Assisted Solution

by:tone28
tone28 earned 50 total points
ID: 17119299
Change

minCol2 = varArray(2).

to

minCol2 = varArray(1)

Tone
0
 

Author Comment

by:rmtogether
ID: 17119399
thanks, it works

by the way, how can I modify it if I want to get both max and min values for both columns? like below

Maximum Value From Col1
Minimum Value From Col1

Maximum Value From Col2
Minimum Value From Col2
0
 
LVL 16

Assisted Solution

by:jimbobmcgee
jimbobmcgee earned 100 total points
ID: 17120811
Based on the above code, try:

Dim fileName as string
Dim fileNo as integer

Dim maxCol1 as double
Dim minCol1 as double
Dim maxCol2 as double
Dim minCol2 as double
Dim firstRow as boolean

Dim varString as string
Dim varArray as variant

fileName = "C:\temp\fileName.txt"
fileNo = freefile
open fileName for input as #fileNo

firstRow = false

Do while not eof(fileno)
     line input #fileNo, varString
   varArray = split(varString, " ")
     if (firstRow = false) then
          maxCol1 = varArray(0)
          minCol1 = varArray(0)
          maxCol2 = varArray(1)
          minCol2 = varArray(1)
          firstRow = true
     else
          if (varArray(0) > maxCol1) then
               maxCol1 = varArray(0)
          end if
          if (varArray(0) < minCol1) then
               minCol1 = varArray(1)
          end if
          if (varArray(1) > maxCol2) then
               maxCol2 = varArray(0)
          end if
          if (varArray(1) < minCol2) then
               minCol2 = varArray(1)
          end if
     end if
loop

Debug.print "Maximum Value From Col1 is " & maxCol1
Debug.print "Minimum Value From Col1 is " & minCol1
Debug.print "Maximum Value From Col2 is " & maxCol2
Debug.print "Minimum Value From Col2 is " & minCol2

HTH

J.
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 200 total points
ID: 17121329
I realize you have a solution, but consider the following:
1. you will be storing values in an array after you need them
2. you will be parsing a line needlessly
3. your line parsing requires extra datatype conversions (and variants are slow)
4. your variables' double datatypes are a bit of overkil, given the meager size of your data variables.

Consider the following alternative solutions:
A. Less-than-optimal, comparing every value twice, even if a match is found on the first comparison

    Dim sngMin1 As Single
    Dim sngMax1 As Single
    Dim sngMin2 As Single
    Dim sngMax2 As Single
    Dim sngColumn1 As Single
    Dim sngColumn2 As Single
    Dim intFN As Integer

    intFN = FreeFile
    sngMax1 = -3.402823E+38
    sngMax2 = -3.402823E+38
    sngMin1 = 3.402823E+38
    sngMin2 = 3.402823E+38

    Open "e:\temp\minmax.txt" For Input As #intFN
    Do Until EOF(intFN)
       
        Input #intFN, sngColumn1, sngColumn2
       
        If sngColumn1 > sngMax1 Then sngMax1 = sngColumn1
        If sngColumn1 < sngMin1 Then sngMin1 = sngColumn1
        If sngColumn2 > sngMax2 Then sngMax2 = sngColumn2
        If sngColumn2 < sngMin2 Then sngMin2 = sngColumn2
       
    Loop
   
    Debug.Print "Min1: " & sngMin1, "Max1: " & sngMax1
    Debug.Print "Min2: " & sngMin2, "Max2: " & sngMax2
   
    Close #intFN

=================================
B. comparison and loop optimizations
    Dim sngMin1 As Single
    Dim sngMax1 As Single
    Dim sngMin2 As Single
    Dim sngMax2 As Single
    Dim sngColumn1 As Single
    Dim sngColumn2 As Single
    Dim intFN As Integer

    intFN = FreeFile

    Open "e:\temp\minmax.txt" For Input As #intFN
    Input #intFN, sngColumn1, sngColumn2
    sngMax1 = sngColumn1  'initial values
    sngMin1 = sngColumn1
    sngMax2 = sngColumn2
    sngMin2 = sngColumn2

    Input #intFN, sngColumn1, sngColumn2

    Do While True
       
        Select Case sngColumn1
            Case Is > sngMax1
                sngMax1 = sngColumn1
            Case Is < sngMin1
                sngMin1 = sngColumn1
        End Select
        Select Case sngColumn2
            Case Is > sngMax2
                sngMax2 = sngColumn2
            Case Is < sngMin2
                sngMin2 = sngColumn2
        End Select
       
        If EOF(intFN) Then Exit Do
       
        Input #intFN, sngColumn1, sngColumn2
    Loop
   
    Debug.Print "Min1: " & sngMin1, "Max1: " & sngMax1
    Debug.Print "Min2: " & sngMin2, "Max2: " & sngMax2
   
    Close #intFN

'Note: based on your knowledge of the data or performance profiling, you might reverse the order of these Case clauses, checking for a new MIN value prior to a new Max value
0
 
LVL 45

Expert Comment

by:aikimark
ID: 17121347
You can also use SQL to accomplish this task, treating the text file like a table and using the ISAM drivers built into Jet/ADO.
0
 

Author Comment

by:rmtogether
ID: 17122036
hi, jimbobmcgee

thank you
your code works good, but I think you have little bug in the following. should make the following changes


          If (varArray(0) < minCol1) Then
               minCol1 = varArray(1)   --------------------->minCol1 = varArray(0)
          End If
          If (varArray(1) > maxCol2) Then
               maxCol2 = varArray(0)   --------------------->maxCol2 = varArray(1)
          End If
0
 

Author Comment

by:rmtogether
ID: 17122129
thanks guys,

thanks to all of your helps. I have got all I want.
I have another questions related to this topic. could you guys help me more about my question.

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21921693.html

0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Paint/Redraw window while dragging 16 77
how can i unlock a picture file that was loaded in a picturebox in visual c#? 4 93
Adding to a VBA? 6 70
Problem to With line 4 57
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question