|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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... |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
i already declare i=1...how can i make this code work?
|
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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
|
|
#6
|
|||
|
|||
|
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 Last edited by June7 : September 30th, 2009 at 02:54 PM. |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
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. |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Find min and max value from array data |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|