Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old September 22nd, 2009, 08:43 AM
mien mien is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 9 mien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 7 sec
Reputation Power: 0
Find min and max value from array data

i try to get min and max value from array data but i seem not work.and it give error subscript out of range.here is my coding.

[CODE]
Private Sub getminval_Click()

'Get Minimum and maximum Value
Dim strFileName As String
Dim myFile As Integer
Dim strTextLine As String

Dim dblX(10000) As Long
Dim dblY(10000) As Long
Dim dblZ(10000) As Double

Dim arrText As Variant
Dim strName As String

Dim minva, maxva As Integer
Dim i As Integer
Dim min(10000), max(10000) As Double

strFileName = mstrinpfile
myFile = FreeFile
If strFileName = "" Then
MsgBox "No Data ", vbOKOnly, "HydroLab File Error"
Me.Show
End If

i = 1
min(0) = 1000
max(0) = -1000

Open strFileName For Input As #myFile
Line Input #myFile, strTextLine
Do While Not EOF(myFile)
arrText = Split(strTextLine, ",")

dblZ(i) = Str(arrText(2))

min(i) = dblZ(i)

If min(i) < min(i - 1) Then
min(i) = Val(arrText(2))
minval.Text = min(i)
End If

max(i) = dblZ(i)

If max(i) > max(i - 1) Then
max(i) = Val(arrText(2))
maxval.Text = max(i)
End If

i = i + 1
Loop
End Sub
[CODE]
thanks...

Reply With Quote
  #2  
Old September 22nd, 2009, 01:36 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 536 June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 13 h 58 m 7 sec
Reputation Power: 124
Arrays index numbering start with 0 unless you use Option Base 1 in the procedure header or declare the array variable with lower bound starting at 1. Your code doesn't show either so suspect the 'subscript out of range' error is because you have your index variable start with 1 and the array lowerbound is 0. Check link http://msdn.microsoft.com/en-us/lib...179(VS.60).aspx

Use '[/code]' in the second code tag.

Last edited by June7 : September 22nd, 2009 at 01:40 PM.

Reply With Quote
  #3  
Old September 24th, 2009, 09:55 AM
mien mien is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 9 mien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 7 sec
Reputation Power: 0
i already declare i=1...how can i make this code work?

Reply With Quote
  #4  
Old September 24th, 2009, 01:25 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 536 June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 13 h 58 m 7 sec
Reputation Power: 124
If you want i to start with 1 and you use i to refer to the array index then the array lower bound must start with 1 not 0. The default is 0 unless you use the Option Base 1 in the Declarations section or you declare the array with specific bounds, example: Dim dblArray(1 to 10) As Double

Check out this tutorial:http://patorjk.com/programming/tutorials/vbarrays.htm

Reply With Quote
  #5  
Old September 29th, 2009, 12:43 PM
mien mien is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 9 mien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 7 sec
Reputation Power: 0
i tried this code.but i get new error..type mismatch

Code:
Dim strFileName, strTextLine As String
Dim arrText(0 To 10000) As Variant
Dim dblZ(0 To 10000), min(0 To 10000), max(0 To 10000) As Double
Dim strName As String
Dim minva, maxva, i, myFile As Integer

    strFileName = mstrinpfile
    myFile = FreeFile
    If strFileName = "" Then
    MsgBox "No Data ", vbOKOnly, "Geo-Data Plotter File Error"
    Me.Show
    End If
    

min(0) = 1000
max(0) = -1000
i = 1

    Open strFileName For Input As #myFile
    Line Input #myFile, strTextLine
    Do While Not EOF(myFile)
    arrText(i) = Split(strTextLine, ",")
        
    dblZ(i) = Str(arrText(2))
     
    min(i) = dblZ(i)
       
    If min(i) < min(i - 1) Then
    min(i) = dblZ(i)
    minval.Text = min(i)
    Else
    minval.Text = min(i - 1)
    End If
    
    max(i) = dblZ(i)
    
    If max(i) > max(i - 1) Then
    max(i) = dblZ(i)
    maxval.Text = max(i)
    Else
    maxval.Text = max(i - 1)
    End If

                     
i = i + 1
Loop
End Sub

Reply With Quote
  #6  
Old September 29th, 2009, 04:31 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 536 June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 13 h 58 m 7 sec
Reputation Power: 124
The error happens on what line?

Might also help to show example of the input data.

Now I see how you are using the 0 index in the array so my prior post about the array lower bound really wasn't relevant, sorry.

In case you don't know:
When declaring more than one variable on a single line, you have to explicitely declare each one with a datatype or else will default to variant. So unless you want these to be variant need to do:
Code:
Dim strFileName As String, strTextLine As String
Dim minva As Integer, maxva As Integer, i As Integer, myFile As Integer
Same for the array variables.

Last edited by June7 : September 30th, 2009 at 02:54 PM.

Reply With Quote
  #7  
Old September 30th, 2009, 08:18 AM
mien mien is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 9 mien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 7 sec
Reputation Power: 0
thanks for ur guide...my code still have problem.it seem not give correct answer..it should compare the value to get min and max.

Code:
Private Sub getminval_Click()

'Get Minimum and maximum Value
Dim strFileName As String, strTextLine As String
Dim arrText As Variant
Dim min As Double, max As Double
Dim strName As String
Dim i As Integer, myFile As Integer
Dim x(100000), y(100000), z(100000), diff, minva, maxva As Double

    strFileName = mstrinpfile
    myFile = FreeFile
    If strFileName = "" Then
    MsgBox "No Data ", vbOKOnly, "File Error"
    GoTo 10
    End If
    

i = 1
   
    Open strFileName For Input As #myFile
    
    Do While Not EOF(myFile)
    Input #myFile, x(i), y(i), z(i)
       
        min = z(0)
        max = z(0)
       
        If i > 1 Then
                          
        If z(i) < z(i - 1) Then
            min = z(i)
            minval.Text = min
        Else
            minval.Text = z(i - 1)
        End If
        
        If z(i) > z(i - 1) Then
            max = z(i)
            maxval.Text = maxva
        Else
            maxval.Text = max
        End If
       
    End If

i = i + 1
Loop
10
End Sub


here is the example of the data

Code:
4501.87, -35959.47, -6.3
4501.87, -35959.47, -6.3
4513.21, -35952.61, -6.6
4522.91, -35944.57, -6.5
4532.12, -35936.75, -6.7
4541.03, -35929.66, -6.7
4548.59, -35920.53, -7.0
4556.43, -35910.96, -7.0
4566.46, -35903.94, -6.7

Reply With Quote
  #8  
Old September 30th, 2009, 03:18 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 536 June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 13 h 58 m 7 sec
Reputation Power: 124
Here is what I did with your procedure. It works with the example data.
Code:
Private Sub getminval_Click()

'Get Minimum and maximum Value
Dim strFileName As String, strTextLine As String
Dim arrText As Variant
Dim min As Double, max As Double
Dim strName As String
Dim i As Integer, myFile As Integer
Dim x(100000), y(100000), z(100000), diff, minva, maxva As Double

'example of data
'4501.87, -35959.47, -6.3
'4501.87, -35959.47, -6.3
'4513.21, -35952.61, -6.6
'4522.91, -35944.57, -6.5
'4532.12, -35936.75, -6.7
'4541.03, -35929.66, -6.7
'4548.59, -35920.53, -7.0
'4556.43, -35910.96, -7.0
'4566.46, -35903.94, -6.7

z(1) = -6.3
z(2) = -6.3
z(3) = -6.6
z(4) = -6.5
z(5) = -6.7
z(6) = -6.7
z(7) = -7
z(8) = -7
z(9) = -6.7

'    strFileName = mstrinpfile
'    myFile = FreeFile
'    If strFileName = "" Then
'    MsgBox "No Data ", vbOKOnly, "File Error"
'    GoTo 10
'    End If
  
'    Open strFileName For Input As #myFile
    
'    Do While Not EOF(myFile)
'    Input #myFile, x(i), y(i), z(i)
min = z(1)
max = z(1)

For i = 1 To 9
    If z(i) < min Then
        min = z(i)
    End If
    If z(i) > max Then
        max = z(i)
    End If
       
'    If i > 1 Then
'
'        If z(i) < z(i - 1) Then
'            min = z(i)
'            minval.Text = min
'        Else
'            minval.Text = z(i - 1)
'        End If
'
'        If z(i) > z(i - 1) Then
'            max = z(i)
'            maxval.Text = maxva
'        Else
'            maxval.Text = max
'        End If
'
'    End If
Next
Debug.Print min
Debug.Print max

'Loop
'10
End Sub

Last edited by June7 : September 30th, 2009 at 03:26 PM.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > Find min and max value from array data


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek