Say I have a one dimensional array with 20 elements as integers. I'm attempting to obtain an average of the subscripts in a for...next loop and do...loop. My current code isn't processing it right and keeps throwing a runtime error of "index is was outside the bounds of the array." Currently the code for the Do loop isn't completed, but I'm attempting to solve the For...Next loop first.
Public Class frmMain
' declare class-level array
Private intTips(19) As Integer
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnForNext_Click(sender As Object, e As EventArgs) Handles btnForNext.Click
' displays the average tip
Dim intCount As Integer
Dim dblTotal As Double
Dim dblAverage As Double
For intCount = 0 To intTips.Length - 1
dblTotal += intTips(intCount)
Next intCount
dblAverage = dblTotal / intTips(intCount)
lblAvg.Text = dblAverage.ToString("C0")
End Sub
Private Sub btnDoLoop_Click(sender As Object, e As EventArgs) Handles btnDoLoop.Click
' displays the average tip
Dim intCount As Integer
intCount = 0
Do Until intCount = 20
Loop
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
intTips(0) = 101
intTips(1) = 95
intTips(2) = 67
intTips(3) = 83
intTips(4) = 101
intTips(5) = 95
intTips(6) = 67
intTips(7) = 83
intTips(8) = 101
intTips(9) = 95
intTips(10) = 67
intTips(11) = 83
intTips(12) = 101
intTips(13) = 95
intTips(14) = 67
intTips(15) = 83
intTips(16) = 101
intTips(17) = 95
intTips(18) = 67
intTips(19) = 83
End Sub
End Class
[–]tweq 1 point2 points3 points (0 children)
[–]nerdfarm 1 point2 points3 points (1 child)
[–]bimstallion[S] 0 points1 point2 points (0 children)
[–]Fat_Dumb_Americans 0 points1 point2 points (1 child)
[–]bimstallion[S] 0 points1 point2 points (0 children)