Advertisement
anlyx

4_A_VBNET

Oct 20th, 2020 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.52 KB | None | 0 0
  1. Public Class Form1
  2.     Dim NumOfElements As Integer = 100
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         Me.RichTextBox1.Clear()
  5.  
  6.         Dim ItemList As New List(Of Integer)
  7.         Dim DiscreteDistrDict As New SortedDictionary(Of Integer, Integer)
  8.         Dim Mean As Double = 0
  9.  
  10.         Me.GenerateDataset(ItemList)
  11.         Me.PrintDataset(ItemList)
  12.  
  13.         'MEAN
  14.         Mean = Me.CalculateMean(ItemList)
  15.         'Print MEAN Value
  16.         Me.RichTextBox1.AppendText(vbCrLf & "MEAN (OnlineAlgo) = " & Mean.ToString("F") & vbCrLf)
  17.  
  18.         'DISCRETE DISTRIBUTION
  19.         Me.CalculateDiscreteDistribution(ItemList, DiscreteDistrDict)
  20.         Me.PrintDiscreteDistribution(DiscreteDistrDict)
  21.     End Sub
  22.  
  23.     Private Sub GenerateDataset(ItemList As List(Of Integer))
  24.         Dim R As New Random
  25.         Dim RandItem As Integer
  26.  
  27.         For i As Integer = 1 To NumOfElements
  28.             RandItem = R.Next(0, 11)  'Random Integer From 0 to 10
  29.             ItemList.Add(RandItem)
  30.         Next
  31.     End Sub
  32.  
  33.     Private Sub PrintDataset(ItemList As List(Of Integer))
  34.         Me.RichTextBox1.AppendText("DATASET:" & vbCrLf)
  35.         For i As Integer = 0 To NumOfElements - 1
  36.             Me.RichTextBox1.AppendText(("Item_" & i + 1 & ":").PadRight(12) & ItemList(i) & vbCrLf)
  37.             REM NB:     The first element of a list is pointed by index i=0.
  38.             REM NB2:    The last element of a list is pointed by index i=NumOfElements-1
  39.         Next
  40.     End Sub
  41.  
  42.     Private Function CalculateMean(ItemList As List(Of Integer)) As Double
  43.         Dim CurrMean As Double = 0
  44.         Dim ItemCount As Integer = 0
  45.         Dim WrapperObj As Object    'Used to obtain multiple return values from function
  46.  
  47.         For Each s As Integer In ItemList
  48.             WrapperObj = Me.ArithmeticMean_OnlineAlgo(CurrMean, s, ItemCount)
  49.             CurrMean = WrapperObj(0)
  50.             ItemCount = WrapperObj(1)
  51.         Next
  52.  
  53.         Return CurrMean
  54.     End Function
  55.  
  56.     Private Function ArithmeticMean_OnlineAlgo(CurrMean As Double, NextItem As Integer, ItemCount As Integer) As Object
  57.         Return {CurrMean + (NextItem - CurrMean) / (ItemCount + 1), ItemCount + 1}
  58.     End Function
  59.  
  60.     Private Sub CalculateDiscreteDistribution(ItemList As List(Of Integer), DiscreteDistrDict As SortedDictionary(Of Integer, Integer))
  61.         For Each s As Integer In ItemList
  62.             If DiscreteDistrDict.ContainsKey(s) Then
  63.                 DiscreteDistrDict(s) += 1
  64.             Else
  65.                 DiscreteDistrDict.Add(s, 1)
  66.             End If
  67.         Next
  68.     End Sub
  69.  
  70.     Private Sub PrintDiscreteDistribution(Dict As SortedDictionary(Of Integer, Integer))
  71.         Me.RichTextBox1.AppendText(vbCrLf & "DISCRETE DISTRIBUTION:" & vbCrLf _
  72.                                    & "VALUE".PadRight(8) & "COUNT".PadRight(8) _
  73.                                    & "FREQ.".PadRight(8) & "PERC.".PadRight(8) & vbCrLf)
  74.         For Each s As KeyValuePair(Of Integer, Integer) In Dict
  75.             Me.RichTextBox1.AppendText(s.Key.ToString.PadRight(8) & s.Value.ToString.PadRight(8) _
  76.                                    & (s.Value / NumOfElements).ToString().PadRight(8) _
  77.                                    & ((s.Value / NumOfElements * 100).ToString & "%").PadRight(8) _
  78.                                    & vbCrLf)
  79.         Next
  80.     End Sub
  81.  
  82.     Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
  83.         Me.RichTextBox1.ScrollToCaret()
  84.     End Sub
  85. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement