This visual basic program prompts the user to interactively enter 4 integer values, which the program stores in an array. It should then find the minimum and maximum values stored in the array, as well as the average of the 4 values. The code is
Option Explicit On
Option Strict On
Module BattingAverage
Sub Main()
Const MAX_AVERAGES AS Integer = 3
Dim Averages(MAX_AVERAGES -1) as Double
Dim LoopIndex As Integer
Dim BattingAverage As Double
Dim BattingString As String
Dim Min As Double
Dim Max As Double
Dim Total As Double
Dim Average As Double
For LoopIndex = 0 To MAX_AVERAGES - 1
BattingString = InputBox$("Enter a batting average: ")
BattingAverage = Convert.ToDouble(BattingString)
'Assigning a value to Array
Averages(LoopIndex) += BattingAverage
Next LoopIndex
Min = Averages(0)
Max = Averages(0)
Total = Averages(0)
For LoopIndex = 1 To Averages.length -1
If Averages(LoopIndex) < Min then
Min = Averages(LoopIndex)
Else If Averages(LoopIndex) > Max then
Max = Averages(LoopIndex)
end if
Total += Averages(LoopIndex)
'
Next LoopIndex
Average = Total / MAX_AVERAGES
System.Console.WriteLine("Batting Averages : " & Averages(LoopIndex))
System.Console.WriteLine("Maximum value : " &Max)
System.Console.WriteLine("Minimum value : " &Min)
System.Console.WriteLine("Average : " &Average)
End Sub
End Module
I ran the code but it throws this indexoutofbound exception
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at BattingAverage.Main()
I am not sure how to fix this code. I also think that my code(Averages(LoopIndex) += BattingAverage) to assign a value to array is not right. please help
+=increments the current value,=assigns a new value.