Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' Program: Gas Prices
- ' Enter the gas prices, then on button click
- ' determine highest, lowest, or average
- Public Class GasPrice
- Dim listArray(11) As Decimal
- Dim counter As Integer = 0
- ' Enter button
- Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
- ModifyArray(txtInput.Text) ' pass to array
- txtInput.Clear()
- txtInput.Focus()
- End Sub
- ' Pass to array function
- Sub ModifyArray(ByVal arrayElement As Decimal)
- listArray(counter) = arrayElement
- counter += 1
- lstOutput.Items.Add(arrayElement)
- If counter > listArray.GetUpperBound(0) Then ' Button disable
- btnEnter.Enabled = False
- End If
- End Sub
- ' Highest price button
- Private Sub btnHighestPrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHighestPrice.Click
- Array.Sort(listArray)
- Dim index As Decimal = -1
- Dim max As Decimal = listArray(0)
- For i = 0 To listArray.GetUpperBound(0)
- If (listArray(i) > max) Then
- max = listArray(i)
- index = i
- End If
- Next
- lblAnswer.Text = "Highest price: $" & max & " in month " & index
- End Sub
- ' Lowest price button
- Private Sub btnLowestPrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLowestPrice.Click
- Array.Sort(listArray)
- Dim index As Decimal = 0
- Dim min As Decimal = listArray(0)
- For i = 0 To listArray.GetUpperBound(0)
- If (listArray(i) < min) Then
- min = listArray(i)
- index = i
- End If
- Next
- lblAnswer.Text = "Lowest price: $" & min & " in month " & index
- End Sub
- ' Average price button
- Private Sub btnAveragePrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAveragePrice.Click
- Dim counter As Decimal
- Dim total As Decimal = 0
- Dim average As Decimal
- For counter = 0 To UBound(listArray)
- total = total + listArray(counter)
- Next
- average = total / UBound(listArray)
- lblAnswer.Text = "Average price: $" & average
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement