Question
I am trying to complete this assignment. All I need to do is find a way to search through the rainfall amunt array, match it
I am trying to complete this assignment. All I need to do is find a way to search through the rainfall amunt array, match it up to the month array and display it with the respective min or max amount on the form. I'm stuck on how to set up a function to do this. My code is as follows:
Public Class Form1 ' This application calculates and displays rainfall statistics
' Class-level declarations Const intMAX_SUBSCRIPT As Integer = 2 ' Upper sibscript Dim strMonths(intMAX_SUBSCRIPT) As String ' Month names Dim dblRain(intMAX_SUBSCRIPT) As Double ' Rainfall amount
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Initialize the month array with data. initArray()
'InitializeComponent the monthly rainfall list box. lstRain.Items.Add("Monthly Rainfall Input") lstRain.Items.Add("- - - - - - - - - - - - - - - - - - - - - - ") End Sub Private Sub initArray() ' Months January through December. strMonths(0) = "January" strMonths(1) = "February" strMonths(2) = "March" 'strMonths(3) = "April" 'strMonths(4) = "May" 'strMonths(5) = "June" 'strMonths(6) = "July" 'strMonths(7) = "August" 'strMonths(8) = "September" 'strMonths(9) = "October" 'strMonths(10) = "November" 'strMonths(11) = "December" End Sub
Private Sub btnInPut_Click(sender As Object, e As EventArgs) Handles btnInPut.Click Dim intCount As Integer = 0 'Loop Counter Do While intCount <= intMAX_SUBSCRIPT Try ' Get the rainfall amount per month. dblRain(intCount) = CInt( InputBox("Enter the total inches of rainfall fort the month " & strMonths(intCount)))
' Check for numbers 0 and above. If CInt(dblRain(intCount)) < 0 Then MessageBox.Show("Your rainfall amount must be 0 or more>") Else ' Display monthly rainfall. lstRain.Items.Add("Rainfall for " & strMonths(intCount) & " " & "= " & (dblRain(intCount)))
' Increment intcount. intCount += 1 End If Catch ' Error message for invalid input. MessageBox.Show("Enter a valid integer.") End Try Loop End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim dblTotalRainfall As Double ' To hold total rainfall. Dim dblAverageRainfall As Double ' To hold average rainfall. Dim dblLowestRainfall As Double ' To hold the minimum rainfall. Dim dblHighestRainfall As Double ' To hold the maximum rainfall. ' Dim intMinValue As Integer ' To hold the index of the minmum value in the dblRain array. ' Dim dblLowest As Double
' Get the total rainfall, average, minimum, and maximum amounts and location of the minimum and maximum amounts. dblTotalRainfall = TotalArray(dblRain) dblAverageRainfall = AverageArray(dblRain) dblLowestRainfall = Lowest(dblRain) dblHighestRainfall = Highest(dblRain) 'intMinValue = FindMinValue(dblRain, dblLowestRainfall)
' Display the results. lblTotal.Text = ("The total annual rainfall was " & dblTotalRainfall.ToString("n")) lblAvg.Text = ("The average monthly rainfall was " & dblAverageRainfall.ToString("n2")) lblMin.Text = ("The minimum monthly rainfall was " & dblLowestRainfall.ToString("n")) ' & " ( " & strMonths(dblRain.Item).ToString & )") lblMax.Text = ("The maximum monthly rainfall was " & dblHighestRainfall.ToString("n")) '& " ( " & strMonths(intCount) & )") End Sub
Function TotalArray(ByVal dblRain() As Double) As Double Dim dblTotal As Double = 0 ' Accumulator Dim intCount As Integer ' Loop counter
' Calculate the total of the arrays elements For intCount = 0 To (dblRain.Length - 1) dblTotal += dblRain(intCount) Next
' Return the total Return dblTotal End Function
Function AverageArray(ByVal dblRain() As Double) As Double Return TotalArray(dblRain) / dblRain.Length End Function
Function Lowest(ByVal dblRain() As Double) As Double Dim intCount As Integer ' Loop counter Dim dblLowest As Double ' To hold the lowest value
' Get the first value in the array. dblLowest = dblRain(0)
' Search for the lowest value. For intCount = 1 To (dblRain.Length - 1) If dblRain(intCount) < dblLowest Then dblLowest = dblRain(intCount) End If Next
' Return the lowest value. Return dblLowest End Function
Function FindMinValue(ByVal dblRain() As Double, ByVal dblLowestRainfall As Double) As Integer Dim intCount As Integer 'Loop counter Dim intItem As Integer
For intCount = 0 To (dblRain.Length - 1)
' Check it the value was found. If dblRain(intItem) = dblLowestRainfall Then Return intItem
End If Next
Return intItem End Function Function Highest(ByVal dblRain() As Double) As Double Dim intCount As Integer ' Loop counter Dim dblHighest As Double ' To hold the highest value
' Get the first value in the array. dblHighest = dblRain(0)
' Search for the highest value in the array. For intCount = 1 To (dblRain.Length - 1) If dblRain(intCount) > dblHighest Then dblHighest = dblRain(intCount) End If Next
' Return the highest value. Return dblHighest End Function Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click ' Clear the list box and the four statistic lables.
lstRain.Items.Clear() lblTotal.Text = String.Empty lblAvg.Text = String.Empty lblMin.Text = String.Empty lblMax.Text = String.Empty btnInPut.Focus() End Sub
Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click ' Close the form. Me.Close() End Sub End Class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started