Jarvis0007 Jarvis0007 - 7 months ago 26
Vb.net Question

Visual Basics: Rainfall Stats

I am trying to figure out what I am missing. I have already submitted this homework assignment as you see it. When returning the rainfall min and max stats, you should also be bringing over what month goes with the min and max. I have try many different ways but nothing seems to work. What am I missing? No rush, as I it was due April 19th, but I have already submitted it. Just trying to see where I have gone wrong.

Const intMONTHS As Integer = 11
Dim sngRain(intMONTHS) As Single
Dim strMonths() As String = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November",
"December"}
Private Sub btnInput_Click(sender As Object, e As EventArgs) Handles btnInput.Click
lstOutput.Items.Add("Monthly Rainfall Input")
lstOutput.Items.Add("___________________________")
GetRainAmount(sngRain)

End Sub

Sub GetRainAmount(ByRef sngRain() As Single)
Dim intCount As Integer = 0
Do While intCount < sngRain.Length
Try
sngRain(intCount) =
CDec(InputBox("Please enter the amount of Rain(in inches) for " & strMonths(intCount)))
lstOutput.Items.Add(("Rainfall for " & strMonths(intCount) & " = " & sngRain(intCount)))
intCount += 1
Catch ex As Exception
MessageBox.Show("Enter a valid numeric value.")
End Try
Loop
End Sub

Function Total(ByVal sngRain() As Single) As Single
Dim sngTotal As Single = 0
Dim intCount As Integer

For intCount = 0 To (sngRain.Length - 1)
sngTotal += sngRain(intCount)

Next
Return sngTotal
End Function

Function Average(ByVal sngRain() As Single) As Single
Return Total(sngRain) / strMonths.Length
End Function

Function Minimum(ByVal sngRain() As Single) As Single
Dim intCount As Integer
Dim sngMinimum As Single
Dim intminIndex As Integer

sngMinimum = sngRain(0)

For intCount = 0 To (sngRain.Length - 1)
If sngRain(intCount) < sngMinimum Then
sngMinimum = sngRain(intCount)
intminIndex = intCount
End If
Next
Return sngMinimum

End Function
Function Maximum(ByVal sngRain() As Single) As Single
Dim intCount As Integer
Dim sngMaximum As Single
Dim intmaxIndex As Integer

sngMaximum = sngRain(0)

For intCount = 0 To (sngRain.Length - 1)
If sngRain(intCount) > sngMaximum Then
sngMaximum = sngRain(intCount)
intmaxIndex = CInt(strMonths(intCount))
End If
Next
Return sngMaximum

End Function

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim intminIndex As Integer
Dim intmaxIndex As Integer
Dim sngTotal As Single
Dim sngAverage As Single
Dim sngMinimum As Single
Dim sngMaximum As Single

sngTotal = Total(sngRain)
sngAverage = Average(sngRain)
sngMinimum = Minimum(sngRain)
sngMaximum = Maximum(sngRain)

lblTotalRain.Text = ("The total annual rainfall was " & sngTotal)
lblAverage.Text = ("The average monthly rainfall was " & sngAverage)
lblMinimum.Text = ("The minimum monthly rainfall was " & sngMinimum & "(" & strMonths(intminIndex) & ")")
lblMaximum.Text = ("The maximum monthly rainfall was " & sngMaximum & "(" & strMonths(intmaxIndex) & ")")
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lstOutput.Items.Clear()
lblTotalRain.Text = String.Empty
lblAverage.Text = String.Empty
lblMinimum.Text = String.Empty
lblMaximum.Text = String.Empty

End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub


End Class

Answer

You appear to be properly setting intmaxIndex in the Maximum function and intminIndex in the Minimum function, but those values are local to each function. Those variables in btnDisplay_Click are then not being set.

You can get the min and max month index by passing the intminIndex and intmaxIndex variables ByRef into the Minimum and Maximum functions respectively.

Function Minimum(ByVal sngRain() As Single, ByRef intminIndex as Integer) As Single
    Dim intCount As Integer
    Dim sngMinimum As Single

    sngMinimum = sngRain(0)

    For intCount = 0 To (sngRain.Length - 1)
        If sngRain(intCount) < sngMinimum Then
            sngMinimum = sngRain(intCount)
            intminIndex = intCount
        End If
    Next
    Return sngMinimum

End Function
Function Maximum(ByVal sngRain() As Single, ByRef intmaxIndex as Integer) As Single
    Dim intCount As Integer
    Dim sngMaximum As Single

    sngMaximum = sngRain(0)

    For intCount = 0 To (sngRain.Length - 1)
        If sngRain(intCount) > sngMaximum Then
            sngMaximum = sngRain(intCount)
            intmaxIndex = intCount
        End If
    Next
    Return sngMaximum

End Function

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    Dim intminIndex As Integer = 0
    Dim intmaxIndex As Integer = 0
    Dim sngTotal As Single
    Dim sngAverage As Single
    Dim sngMinimum As Single
    Dim sngMaximum As Single

    sngTotal = Total(sngRain)
    sngAverage = Average(sngRain)
    sngMinimum = Minimum(sngRain, intminIndex)
    sngMaximum = Maximum(sngRain, intmaxIndex)

    lblTotalRain.Text = ("The total annual rainfall was " & sngTotal)
    lblAverage.Text = ("The average monthly rainfall was " & sngAverage)
    lblMinimum.Text = ("The minimum monthly rainfall was " & sngMinimum & "(" & strMonths(intminIndex) & ")")
    lblMaximum.Text = ("The maximum monthly rainfall was " & sngMaximum & "(" & strMonths(intmaxIndex) & ")")
End Sub
Comments