Question: IN VISUAL BASIC This application lets the user enter the rainfall for each of 12 months into an array. When the user clicks on the
IN VISUAL BASIC

This application lets the user enter the rainfall for each of 12 months into an array. When the user clicks on the Display Stats button, the application should calculate and display the following statistics: Total rainfall for the year, the average monthly rainfall, and the months with the hights and lowest amounts of rainfall. The example above shows the applications form after each months rainfall amount has been entered and the statistics have been displayed.
The application is complete except for the Display Stats button Click Event Procedure. There is an array called strMonthNames() with 12 elements containing the names of the 12 months. There is a parallel array containing the rainfall for each month called monthlyRain(). Element 0 in monthlyRain contains the rainfall for January, element 1 in monthlyRain contains the rainfall for February, etc. The values for these arrays will be loaded into the arrays when the user launches the application, clicks on the Input Monthly Rainfall button, and enters the rainfall for each month. The arrays are declared as class level variables and are available to all procedures in the form.
Create the btnStats Click Event Procedure so that the data in these arrays is used to compute four statistics:
the total annual rainfall
the average monthly rainfall
the minimum monthly rainfall
the maximum monthly rainfall
The btnStats Click Event Procedure should display the four statistics, along with explanatory text, in the four form labels as shown.
Create the click event procedure, copy, and paste it into the answer window for this question.
HINTS
Remember that the first subscript value for every array is 0. This means that the maximum subscript value in a 12-element array is 11.
An excellent way to process every element in an array, one at a time, is with a ForNext loop. For example, the following loop will determine the number of array elements that contain a number greater than 10 (the array in this case has 20 elements):
Dim intCount as Integer = 0 For intIndex = 0 to 19 If decNumbers(intIndex) > 10 Then intCount += 1 EndIf Next
To calculate the total rainfall, loop through the montlyRain array, adding the value of each element to an accumulator variable during each pass.
To calculate the average monthly rainfall, divide the total that you just calculated by the number of months.
To calculate the minimum monthly rainfall, create a variable to store this amount and set it to the value stored in element 0 of the monthlyRain array. Next, loop through element 1 through 11 of the array, comparing each element to the variable and update the variable if and when an array element has a lower value.
To calculate the maximum monthly rainfall, create a variable to store this amount and set it to the value stored in element 0 of the monthlyRain array. Next, loop through element 1 through 11 of the array, comparing each element to the variable and update the variable if and when an array element has a higher value.
-----------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub btnInput_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnInput.Click
' Input rainfall for each month, require non-negative numbers,
' and display input on the form for the user to see
Dim intMonth As Integer
Dim strInput As String
lstInput.Items.Add("Monthly Rainfall Input")
lstInput.Items.Add("----------------------------------")
For intMonth = 0 To intMAX_SUBSCRIPT
strInput = InputBox("Please enter the rainfall for " _
& strMonthNames(intMonth) & ":")
' If the user clicks the Cancel button or enters an
' empty string, leave this Click handler immediately.
If strInput.Length = 0 Then
Return
Else
Do While Not IsNumeric(strInput) OrElse CStr(strInput)
strInput = InputBox("Please reenter the rainfall for " _
& strMonthNames(intMonth) & " as a non-negative number:")
' Exit if the user clicks the Cancel button
If strInput.Length = 0 Then Return
Loop
monthlyRain(intMonth) = CStr(strInput)
lstInput.Items.Add(" " & monthlyRain(intMonth) & " for " _
& strMonthNames(intMonth))
End If
Next intMonth
btnStats.Focus()
End Sub
Rainfall Statistics Monthly Rainfall Input 3 for January 8 for Februany 6 for March 4 for April 7for May 5 for June 6 for July 3 for August 8 for September 2 for October 4 for November 3 for December Thetotal annual rainfall was 59 The average monthly rainfall was 4.92 The minimum monthly rainfall was 2 (October) The maximum monthly rainfall was 8 (February) Input Monthly RainfallDisplay Stats Clear Exit
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
