slammer14
asked on
redim array problem
Having problem with reading a sequential access file into an array and calculating average and high score.
i think its the way i declare the redim?
Any suggestions??
Slammer14
Option Explicit
'Dim intScoreArray(1 To 20) As Integer
Dim intNumData As Integer
Dim intRecCount As Integer
Private Sub cmdAvg_Click()
Dim intNum As Integer, intTotal As Integer, sngAvg As Single
For intNum = 1 To intRecCount
intTotal = intTotal + intScoreArray(intNum) 'accumulate array scores
Next intNum
sngAvg = intTotal / intRecCount 'calculate average
lblMsg.Caption = "Average" 'display results
lblAnswer.Caption = Format(sngAvg, "standard")
End Sub
Private Sub cmdExit_Click()
Unload frmTest
End Sub
Private Sub cmdHigh_Click()
Dim intNum As Integer, intHigh As Integer
intHigh = intScoreArray(1) 'initialize intHigh to first array element
For intNum = 2 To intNumData 'compare intHigh to remaining array elements
If intScoreArray(intNum) > intHigh Then 'element is greater than intHigh
intHigh = intScoreArray(intNum)
End If
Next intNum
lblMsg.Caption = "Highest" 'display results
lblAnswer.Caption = intHigh
End Sub
Private Sub cmdUpdate_Click()
Dim intNum As Integer, intIncrease As Integer
intIncrease = Val(InputBox("Enter the number of additional points:"))
For intNum = 1 To intNumData 'add additional points to each score in the array
intScoreArray(intNum) = intScoreArray(intNum) + intIncrease
Next intNum
lblMsg.Caption = "Scores updated by" 'display message
lblAnswer.Caption = intIncrease
Open "a:\Tut10\Test11.dat" For Output As #1 'open file
For intNum = 1 To intNumData 'write array contents to file
Write #1, intScoreArray(intNum)
Next intNum
Close #1 'close file
End Sub
Private Sub Form_Load()
frmTest.Left = (Screen.Width - frmTest.Width) / 2
frmTest.Top = (Screen.Height - frmTest.Height) / 2
Dim intNum As Integer, strNext As String, intRecord As Integer
Dim i As Integer
Open "a:\Tut10\test11.dat" For Input As #1 'open file
intRecord = 0
Do While Not EOF(1)
ReDim intScoreArray(0) As Integer
Input #1, intNum
ReDim Preserve intScoreArray(1 To intNum) As Integer
For i = 1 To intNumData
Input #1, intScoreArray(i)
intRecCount = intRecCount + 1 ' establish amount of records for upperbound
Next
Loop
'
Close #1
End Sub
i think its the way i declare the redim?
Any suggestions??
Slammer14
Option Explicit
'Dim intScoreArray(1 To 20) As Integer
Dim intNumData As Integer
Dim intRecCount As Integer
Private Sub cmdAvg_Click()
Dim intNum As Integer, intTotal As Integer, sngAvg As Single
For intNum = 1 To intRecCount
intTotal = intTotal + intScoreArray(intNum) 'accumulate array scores
Next intNum
sngAvg = intTotal / intRecCount 'calculate average
lblMsg.Caption = "Average" 'display results
lblAnswer.Caption = Format(sngAvg, "standard")
End Sub
Private Sub cmdExit_Click()
Unload frmTest
End Sub
Private Sub cmdHigh_Click()
Dim intNum As Integer, intHigh As Integer
intHigh = intScoreArray(1) 'initialize intHigh to first array element
For intNum = 2 To intNumData 'compare intHigh to remaining array elements
If intScoreArray(intNum) > intHigh Then 'element is greater than intHigh
intHigh = intScoreArray(intNum)
End If
Next intNum
lblMsg.Caption = "Highest" 'display results
lblAnswer.Caption = intHigh
End Sub
Private Sub cmdUpdate_Click()
Dim intNum As Integer, intIncrease As Integer
intIncrease = Val(InputBox("Enter the number of additional points:"))
For intNum = 1 To intNumData 'add additional points to each score in the array
intScoreArray(intNum) = intScoreArray(intNum) + intIncrease
Next intNum
lblMsg.Caption = "Scores updated by" 'display message
lblAnswer.Caption = intIncrease
Open "a:\Tut10\Test11.dat" For Output As #1 'open file
For intNum = 1 To intNumData 'write array contents to file
Write #1, intScoreArray(intNum)
Next intNum
Close #1 'close file
End Sub
Private Sub Form_Load()
frmTest.Left = (Screen.Width - frmTest.Width) / 2
frmTest.Top = (Screen.Height - frmTest.Height) / 2
Dim intNum As Integer, strNext As String, intRecord As Integer
Dim i As Integer
Open "a:\Tut10\test11.dat" For Input As #1 'open file
intRecord = 0
Do While Not EOF(1)
ReDim intScoreArray(0) As Integer
Input #1, intNum
ReDim Preserve intScoreArray(1 To intNum) As Integer
For i = 1 To intNumData
Input #1, intScoreArray(i)
intRecCount = intRecCount + 1 ' establish amount of records for upperbound
Next
Loop
'
Close #1
End Sub
Do While Not EOF(1)
ReDim intScoreArray(0) As Integer
Input #1, intNum
ReDim Preserve intScoreArray(1 To intNum) As Integer
For i = 1 To intNumData
Input #1, intScoreArray(i)
intRecCount = intRecCount + 1 ' establish amount of records for upperbound
Next
Loop
I dont see why you are using two Redim statements here.
ReDim intScoreArray(0) As Integer
Input #1, intNum
ReDim Preserve intScoreArray(1 To intNum) As Integer
For i = 1 To intNumData
Input #1, intScoreArray(i)
intRecCount = intRecCount + 1 ' establish amount of records for upperbound
Next
Loop
I dont see why you are using two Redim statements here.
also you state For i = 1 to intNumData in your form load event. I see where you declare intNumData but I dont see anywhere where you assign a value to it.
Too much to comment on.. take a look at this.. <huge smile>.. Great to SIEZE ya again !!!
<----- Code Begin ----->
Option Explicit
Private m_intCount As Integer
Private m_intScores(1 To 20) As Integer
Private m_strFilePath As String
Private Sub cmdAvg_Click()
Dim intTotal As Integer
Dim intIndex As Integer
For intIndex = 1 To m_intCount
intTotal = intTotal _
+ m_intScores(intIndex) ' accumulate array scores
Next intIndex
lblMsg.Caption = "Average" ' display results
Dim sngAverage As Single
sngAverage = intTotal / m_intCount ' calculate average
lblAnswer.Caption = Format(sngAverage, "standard")
End Sub
Private Sub cmdExit_Click()
Unload frmTest
End Sub
Private Sub cmdHigh_Click()
Dim intMaximum As Integer
intMaximum = m_intScores(1) ' initialize intMaximum to first array element
Dim intIndex As Integer
For intIndex = 2 To m_intCount ' compare intMaximum to remaining array elements
If m_intScores(intIndex) > intMaximum _
Then 'element is greater than intMaximum
intMaximum = m_intScores(intIndex)
End If
Next intIndex
lblMsg.Caption = "Highest" ' display results
lblAnswer.Caption = intMaximum
End Sub
Private Sub cmdUpdate_Click()
Dim intTotal As Integer
Dim intIndex As Integer
For intIndex = 1 To m_intCount ' add additional points to each score in the array
m_intScores(intIndex) = Val(InputBox( _
"Enter the number of points:", _
"Entry# " & intIndex & " Of " & m_intCount, _
m_intScores(intIndex) _
))
intTotal = intTotal + m_intScores(intIndex)
Next intIndex
lblMsg.Caption = "Scores Total " ' display message
lblAnswer.Caption = intTotal
On Error Resume Next
Kill m_strFilePath
On Error GoTo 0
Open m_strFilePath For Output As #1 ' open file
For intIndex = 1 To m_intCount ' write array contents to file
Write #1, m_intScores(intIndex)
Next intIndex
Close #1 ' close file
End Sub
Private Sub Form_Load()
frmTest.Left = (Screen.Width - frmTest.Width) / 2
frmTest.Top = (Screen.Height - frmTest.Height) / 2
m_strFilePath = "a:\Tut10\test11.dat"
Dim intTotal As Integer
On Error Resume Next
Open m_strFilePath For Input As #1 ' open file
If Err.Number > 0 _
Then
Err.Clear
Else
Dim intIndex As Integer
Do Until EOF(1) _
Or intIndex = UBound(m_intScores)
intIndex = intIndex + 1
Input #1, m_intScores(intIndex)
Loop
End If
Close #1
lblMsg.Caption = "Scores Loaded " ' display message
lblAnswer.Caption = intIndex
m_intCount = UBound(m_intScores)
End Sub
<----- Code End ----->
<----- Code Begin ----->
Option Explicit
Private m_intCount As Integer
Private m_intScores(1 To 20) As Integer
Private m_strFilePath As String
Private Sub cmdAvg_Click()
Dim intTotal As Integer
Dim intIndex As Integer
For intIndex = 1 To m_intCount
intTotal = intTotal _
+ m_intScores(intIndex) ' accumulate array scores
Next intIndex
lblMsg.Caption = "Average" ' display results
Dim sngAverage As Single
sngAverage = intTotal / m_intCount ' calculate average
lblAnswer.Caption = Format(sngAverage, "standard")
End Sub
Private Sub cmdExit_Click()
Unload frmTest
End Sub
Private Sub cmdHigh_Click()
Dim intMaximum As Integer
intMaximum = m_intScores(1) ' initialize intMaximum to first array element
Dim intIndex As Integer
For intIndex = 2 To m_intCount ' compare intMaximum to remaining array elements
If m_intScores(intIndex) > intMaximum _
Then 'element is greater than intMaximum
intMaximum = m_intScores(intIndex)
End If
Next intIndex
lblMsg.Caption = "Highest" ' display results
lblAnswer.Caption = intMaximum
End Sub
Private Sub cmdUpdate_Click()
Dim intTotal As Integer
Dim intIndex As Integer
For intIndex = 1 To m_intCount ' add additional points to each score in the array
m_intScores(intIndex) = Val(InputBox( _
"Enter the number of points:", _
"Entry# " & intIndex & " Of " & m_intCount, _
m_intScores(intIndex) _
))
intTotal = intTotal + m_intScores(intIndex)
Next intIndex
lblMsg.Caption = "Scores Total " ' display message
lblAnswer.Caption = intTotal
On Error Resume Next
Kill m_strFilePath
On Error GoTo 0
Open m_strFilePath For Output As #1 ' open file
For intIndex = 1 To m_intCount ' write array contents to file
Write #1, m_intScores(intIndex)
Next intIndex
Close #1 ' close file
End Sub
Private Sub Form_Load()
frmTest.Left = (Screen.Width - frmTest.Width) / 2
frmTest.Top = (Screen.Height - frmTest.Height) / 2
m_strFilePath = "a:\Tut10\test11.dat"
Dim intTotal As Integer
On Error Resume Next
Open m_strFilePath For Input As #1 ' open file
If Err.Number > 0 _
Then
Err.Clear
Else
Dim intIndex As Integer
Do Until EOF(1) _
Or intIndex = UBound(m_intScores)
intIndex = intIndex + 1
Input #1, m_intScores(intIndex)
Loop
End If
Close #1
lblMsg.Caption = "Scores Loaded " ' display message
lblAnswer.Caption = intIndex
m_intCount = UBound(m_intScores)
End Sub
<----- Code End ----->
Try this
Option Base 1
Dim intScoreArray() As Integer
to redim the array:
ReDim Preserve intScoreArray(intNum)
To erase the array
erase intScoreArray
Option Base 1
Dim intScoreArray() As Integer
to redim the array:
ReDim Preserve intScoreArray(intNum)
To erase the array
erase intScoreArray
Redim will give you error in the following case
Dim iaScore(4) as integer
....
....
....
....
Redim iaScore(20) as integer
-- error because iaScore should not be dimensioned when declare the dimension statement should be dim a() as integeriin that case it will work.
Dim iaScore(4) as integer
....
....
....
....
Redim iaScore(20) as integer
-- error because iaScore should not be dimensioned when declare the dimension statement should be dim a() as integeriin that case it will work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i tried it like this and it gave error.
if array is dimintioned ex.
Dim r(4) as integer and then you redim you get an error array dimentioned
but if array was declared without given dimention like
Dim r() as integer and then you redim it
Redim r(1 to 10) as integer
no error will occur.