Solved

find maximum and minimum from text file

Posted on 2006-07-16
10
328 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 6

Assisted Solution

by:tone28
tone28 earned 50 total points
Comment Utility
Change

minCol2 = varArray(2).

to

minCol2 = varArray(1)

Tone
0
 

Author Comment

by:rmtogether
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Assisted Solution

by:jimbobmcgee
jimbobmcgee earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now