Name: Anonymous 2012-08-17 3:27
I'm taking a VB 2010 class and this assignment is bugging me. Here is the assignement:
[QUOTE]
Rainfall Statistics
Create an application that lets the user enter the rainfall for each of 12 months into an array. The application should calculate and display the following statistics: total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts of rainfall.
[/QUOTE]
I basically have it written and it works except for several issues.
1. My average "decAverageRain" isn't displaying at all. I don't know if I did that function right.
2. The bolded part of the code needs to display the month that has the highest/lowest rain fall. It's not displaying as well and I don't know if I did that right either.
Any help would be appreciated.
[QUOTE]
Rainfall Statistics
Create an application that lets the user enter the rainfall for each of 12 months into an array. The application should calculate and display the following statistics: total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts of rainfall.
[/QUOTE]
Public Class Form1
'Class level declarations
Const intMAX_SUBSCRIPT As Integer = 11 'Upper subscript
Dim decRain(intMAX_SUBSCRIPT) As Decimal 'Rainfall per month
Dim strMonths() 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
'get data from user
GetRainData(decRain)
End Sub
Sub GetRainData(ByRef decRain() As Decimal)
Dim intCount As Integer = 0 'loop
Do While intCount < decRain.Length
Try
decRain(intCount) = CDec(InputBox("Please enter the rainfall in inches for " & strMonths(intCount).ToString()))
intCount += 1
Catch
End Try
Loop
End Sub
'TotalArray accept a decimal array and returns the total of its value
Function TotalArray(ByVal decValues() As Decimal) As Decimal
Dim decTotalRain As Decimal = 0
Dim intCount As Integer
'calculate
For intCount = 0 To (decValues.Length - 1)
decTotalRain += decValues(intCount)
Next
Return decTotalRain
End Function
Function AverageArray(ByVal decValues() As Decimal) As Decimal
Return TotalArray(decValues) / decValues.Length
End Function
Function Highest(ByVal decValues() As Decimal) As Decimal
Dim intCount As Integer
Dim decHighest As Decimal
decHighest = decValues(0)
For intCount = 1 To (decValues.Length - 1)
If decValues(intCount) > decHighest Then
decHighest = decValues(intCount)
End If
Next
Return decHighest
End Function
Function Lowest(ByVal decValues() As Decimal) As Decimal
Dim intCount As Integer
Dim decLowest As Decimal
decLowest = decValues(0)
For intCount = 1 To (decValues.Length - 1)
If decValues(intCount) < decLowest Then
decLowest = decValues(intCount)
End If
Next
Return decLowest
End Function
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim decTotalRain As Decimal 'holds total rain
Dim decAverageRain As Decimal 'holds average rain
Dim decHighestRain As Decimal
Dim decLowestRain As Decimal
'get total, average, high and low
decTotalRain = TotalArray(decRain)
decAverageRain = AverageArray(decRain)
decHighestRain = Highest(decRain)
decLowestRain = Lowest(decRain)
'display information to labels
lblTotalRain.Text = "The total annual rainfall was" & decTotalRain.ToString
lblAverageRain.Text = "The average monthly rainfall was" & decAverageRain.ToString
lblHighestRain.Text = "The maximum monthly rainfall was" & decHighestRain.ToString & " " & [B]strMonths(Array.IndexOf(decRain, decHighestRain))[/B]
lblLowestRain.Text = "The minimum monthly rain fall was" & decLowestRain.ToString
'display to listbox
lstRainfall.Items.Add("Monthly Rainfall Input")
lstRainfall.Items.Add("____________________________")
For intCount = 0 To strMonths.Length - 1
lstRainfall.Items.Add((decRain(intCount)) & " for " & (strMonths(intCount)))
Next
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'Clear the labelBox
lblTotalRain.Text = String.Empty
lblAverageRain.Text = String.Empty
lblHighestRain.Text = String.Empty
lblLowestRain.Text = String.Empty
'clear list
lstRainfall.Items.Clear()
End Sub
End ClassI basically have it written and it works except for several issues.
1. My average "decAverageRain" isn't displaying at all. I don't know if I did that function right.
2. The bolded part of the code needs to display the month that has the highest/lowest rain fall. It's not displaying as well and I don't know if I did that right either.
Any help would be appreciated.