Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '    Program: Gas Prices
  2. '             Enter the gas prices, then on button click
  3. '             determine highest, lowest, or average
  4.  
  5.  
  6. Public Class GasPrice
  7.     Dim listArray(11) As Decimal
  8.     Dim counter As Integer = 0
  9.  
  10.     ' Enter button
  11.    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
  12.         ModifyArray(txtInput.Text) ' pass to array
  13.        txtInput.Clear()
  14.         txtInput.Focus()
  15.     End Sub
  16.  
  17.     ' Pass to array function
  18.    Sub ModifyArray(ByVal arrayElement As Decimal)
  19.         listArray(counter) = arrayElement
  20.         counter += 1
  21.         lstOutput.Items.Add(arrayElement)
  22.  
  23.         If counter > listArray.GetUpperBound(0) Then ' Button disable
  24.            btnEnter.Enabled = False
  25.         End If
  26.     End Sub
  27.  
  28.     ' Highest price button
  29.    Private Sub btnHighestPrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHighestPrice.Click
  30.         Array.Sort(listArray)
  31.         Dim index As Decimal = -1
  32.         Dim max As Decimal = listArray(0)
  33.  
  34.         For i = 0 To listArray.GetUpperBound(0)
  35.             If (listArray(i) > max) Then
  36.                 max = listArray(i)
  37.                 index = i
  38.             End If
  39.         Next
  40.         lblAnswer.Text = "Highest price: $" & max & " in month " & index
  41.     End Sub
  42.  
  43.     ' Lowest price button
  44.    Private Sub btnLowestPrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLowestPrice.Click
  45.         Array.Sort(listArray)
  46.         Dim index As Decimal = 0
  47.         Dim min As Decimal = listArray(0)
  48.  
  49.         For i = 0 To listArray.GetUpperBound(0)
  50.             If (listArray(i) < min) Then
  51.                 min = listArray(i)
  52.                 index = i
  53.             End If
  54.         Next
  55.         lblAnswer.Text = "Lowest price: $" & min & " in month " & index
  56.     End Sub
  57.  
  58.     ' Average price button
  59.    Private Sub btnAveragePrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAveragePrice.Click
  60.         Dim counter As Decimal
  61.         Dim total As Decimal = 0
  62.         Dim average As Decimal
  63.  
  64.         For counter = 0 To UBound(listArray)
  65.             total = total + listArray(counter)
  66.         Next
  67.         average = total / UBound(listArray)
  68.         lblAnswer.Text = "Average price: $" & average
  69.     End Sub
  70. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement