Hello, I\'m taking a class using Visual Basic. We\'re coming towards the end of
ID: 3764055 • Letter: H
Question
Hello, I'm taking a class using Visual Basic. We're coming towards the end of the semester and I'm trying to practice some Program Challenges for our upcoming Final Project. I came across a Program Challenge that could be useful to know when studying for the final. In the text Starting Out With Visual Basic 2012 6th Edition, I'd like to see the solution for Program Challenge #2 "Rainfall Statistics" in Chapter 8. It would be helpful for me since I'm having a particularly hard time with arrays. Thanks in advance!
Explanation / Answer
Dim strMonths() As String = {"January", "February", "March", "April", _
"May", "June", "July", "August", "September", "October", "November", _
"December"}
Dim intMonths(11) As Integer
Private Sub btnRainfall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRainfall.Click
'calculate and display monthly rainfall
Dim intCounter As Integer 'loop counter
Dim intRain As Integer 'amount of rain
lstRainfall.Items.Add("Monthly Rainfall Input")
lstRainfall.Items.Add("___________________________")
'get the rain for each month
Do While intCounter = (0 - 11)
lstRainfall.Items.Add((intRain).ToString() & " for " & strMonths(intCounter))
Try
For intRain = CInt(Val(InputBox("Please Enter the amount of Rainfall in Inches for " &
strMonths(intCounter))))
Next intRain
intMonths(intCounter) = intRain
intCounter = (0 - 11)
Catch
MsgBox("Enter a valid numeric value.")
End Try
Loop
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim intCounter As Integer
Dim intMax As Integer = intMonths(0)
Dim intMin As Integer = intMonths(0)
Dim dblMonthlyavg As Double
Dim intTotal As Integer = 0
Dim minIndex As Integer = 0
Dim maxIndex As Integer = 0
'calculate maximum
For intCounter = 0 To (intMonths.Length - 1)
If intMonths(intCounter) > intMax Then
intMax = intMonths(intCounter)
maxIndex = intCounter
End If
Next intCounter
'calculate(minimum)
For intCounter = 0 To (strMonths.Length - 1)
If intMonths(intCounter) < intMin Then
intMin = intMonths(intCounter)
minIndex = intCounter
End If
Next intCounter
'calculate total rainfall
For intCounter = 0 To (strMonths.Length - 1)
intTotal += intMonths(intCounter)
Next intCounter
'Calculating the Rainfall Average
dblMonthlyavg = (intTotal / strMonths.Length)
txtTotal.Visible = True
txtAverage.Visible = True
txtMin.Visible = True
txtMax.Visible = True
'Filling in Variables to Text Boxes
txtTotal.Text = "The total annual rainfall was " & intTotal
txtAverage.Text = "The average monthly rainfall was " & dblMonthlyavg.ToString("r1")
txtMin.Text = "The minimum monthly rainfall was " & intMin & "(" & strMonths(minIndex) & ")"
txtMax.Text = "The maximum monthly rainfall was " & intMax & "(" & strMonths(maxIndex) & ")"
End Sub
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.