Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to complete this assignment. All I need to do is find a way to searc

ID: 3854124 • Letter: I

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 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

Explanation / Answer

Instead of returning back the value of lowest or highest rainfall, we simply return the index of that value. Using that index, we would be able to get both the month name as well as the corresponding rainfall amount. I have modified the code to do this and cleaned it up. Please check and post a comment in case of any issues. If the answer helped , please rate it. Thank you.

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 intLowIndex As Integer ' To hold the index minimum rainfall.
Dim intHighIndex As Integer ' To hold the index maximum rainfall.
  
' Get the total rainfall, average, minimum, and maximum amounts and location of the minimum and maximum amounts.
dblTotalRainfall = TotalArray(dblRain)
dblAverageRainfall = AverageArray(dblRain)
intLowIndex = LowestIndex(dblRain)
intHighIndex = HighestIndex(dblRain)

' 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 " & dblRain(intLowIndex).ToString("n") & " ( " & strMonths(intLowIndex) & " )")
lblMax.Text = ("The maximum monthly rainfall was " & dblRain(intHighIndex).ToString("n") & " ( " & strMonths(intHighIndex) & " )")
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 LowestIndex(ByVal dblRain() As Double) As Integer
Dim intCount As Integer ' Loop counter
Dim intLowestIndex As Integer ' To hold the lowest value
' Assume the first value in the array to be lowest
intLowestIndex = 0
' Search for the lowest value.
For intCount = 1 To (dblRain.Length - 1)
If dblRain(intCount) < dblRain(intLowestIndex) Then
intLowestIndex = intCount
End If
Next
' Return the lowest value.
Return intLowestIndex
End Function

Function HighestIndex(ByVal dblRain() As Double) As Integer
Dim intCount As Integer ' Loop counter
Dim intHighestIndex As Integer ' To hold the highest value
' Assume the first value in the array is the highest
intHighestIndex = 0
' Search for the highest value in the array.
For intCount = 1 To (dblRain.Length - 1)
If dblRain(intCount) > dblRain(intHighestIndex) Then
intHighestIndex = dblRain(intCount)
End If
Next
' Return the highest index.
Return intHighestIndex
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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote