Question
Design a VB program to allow user to enter rainfall for all 12 months into array. Program should calculate and display the total rainfall for the year, the average monthly and the months with the highest and lowest amounts.
Explanation / Answer
Public Class Form1 ' Autumn Brown ' 11/25/2013 ' Rainfall Statistics ' class level declarations Dim intCount As Integer Dim intRainfall() As Integer Dim strMonth() As String = {"January", "February", "March", "April", _ "May", "June", "July", "August", "September", "October", "November", _ "December"} Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click Dim intCount As Integer ' counter Dim intRainfall(11) As Integer ' amount of rainfall Dim strMsg, strInput As String ' pop-up message and user input For intCount = 0 To 11 ' create message asking user to enter rainfall strMsg = "Enter the amount of rainfall for the month of " & strMonth(intCount) Dim blnValueOk As Boolean = False ' allow user to enter data, determine that value is numeric ' add data to the list and the array Do While blnValueOk = False strInput = InputBox(strMsg, "Input Rainfall") Try intRainfall(intCount) = CInt(strInput) blnValueOk = True Catch ex As Exception MessageBox.Show("That value was nonnumeric." _ & "Please try again.", "Error") End Try Loop ListBox1.Items.Add((intRainfall(intCount)) & " for " & strMonth(intCount).ToString) Next intCount lblAverage.Text = "The average monthly rainfall was " & getAverage(intRainfall) lblMax.Text = "The maximum monthly rainfall was " & getMaximum(intRainfall) lblTotal.Text = "The total annual rainfall was " & getTotal(intRainfall) lblMin.Text = "The minimum monthly rainfall was " & getMinimum(intRainfall) End Sub Public Function getAverage(ByRef rainArray() As Integer) As Decimal ' calculate average Dim intCount As Integer Dim intTotal As Integer = 0 Dim decAverage As Decimal For intCount = 0 To (strMonth.Length - 1) intTotal += rainArray(intCount) decAverage = CInt(intTotal / 12) Next intCount Return decAverage End Function Public Function getMaximum(ByVal rainAmountArray() As Integer) As Integer ' find maximum Dim intCount As Integer Dim intMax As Integer intMax = rainAmountArray(0) For intCount = 1 To 11 If rainAmountArray(intCount) > intMax Then intMax = rainAmountArray(intCount) End If Next Return intMax End Function Public Function getTotal(ByVal rainAmountArray() As Integer) As Integer ' calculate total Dim intCount As Integer Dim decTotal As Integer = 0 For intCount = 0 To (strMonth.Length - 1) decTotal += rainAmountArray(intCount) Next intCount Return decTotal End Function Public Function getMinimum(ByVal rainAmountArray() As Integer) As Integer ' find minimum Dim intCount As Integer Dim intMin As Integer = rainAmountArray(0) For intCount = 1 To (strMonth.Length - 1) If rainAmountArray(intCount)