Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Dim NumOfElements As Integer = 100
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Me.RichTextBox1.Clear()
- Dim ItemList As New List(Of Integer)
- Dim DiscreteDistrDict As New SortedDictionary(Of Integer, Integer)
- Dim Mean As Double = 0
- Me.GenerateDataset(ItemList)
- Me.PrintDataset(ItemList)
- 'MEAN
- Mean = Me.CalculateMean(ItemList)
- 'Print MEAN Value
- Me.RichTextBox1.AppendText(vbCrLf & "MEAN (OnlineAlgo) = " & Mean.ToString("F") & vbCrLf)
- 'DISCRETE DISTRIBUTION
- Me.CalculateDiscreteDistribution(ItemList, DiscreteDistrDict)
- Me.PrintDiscreteDistribution(DiscreteDistrDict)
- End Sub
- Private Sub GenerateDataset(ItemList As List(Of Integer))
- Dim R As New Random
- Dim RandItem As Integer
- For i As Integer = 1 To NumOfElements
- RandItem = R.Next(0, 11) 'Random Integer From 0 to 10
- ItemList.Add(RandItem)
- Next
- End Sub
- Private Sub PrintDataset(ItemList As List(Of Integer))
- Me.RichTextBox1.AppendText("DATASET:" & vbCrLf)
- For i As Integer = 0 To NumOfElements - 1
- Me.RichTextBox1.AppendText(("Item_" & i + 1 & ":").PadRight(12) & ItemList(i) & vbCrLf)
- REM NB: The first element of a list is pointed by index i=0.
- REM NB2: The last element of a list is pointed by index i=NumOfElements-1
- Next
- End Sub
- Private Function CalculateMean(ItemList As List(Of Integer)) As Double
- Dim CurrMean As Double = 0
- Dim ItemCount As Integer = 0
- Dim WrapperObj As Object 'Used to obtain multiple return values from function
- For Each s As Integer In ItemList
- WrapperObj = Me.ArithmeticMean_OnlineAlgo(CurrMean, s, ItemCount)
- CurrMean = WrapperObj(0)
- ItemCount = WrapperObj(1)
- Next
- Return CurrMean
- End Function
- Private Function ArithmeticMean_OnlineAlgo(CurrMean As Double, NextItem As Integer, ItemCount As Integer) As Object
- Return {CurrMean + (NextItem - CurrMean) / (ItemCount + 1), ItemCount + 1}
- End Function
- Private Sub CalculateDiscreteDistribution(ItemList As List(Of Integer), DiscreteDistrDict As SortedDictionary(Of Integer, Integer))
- For Each s As Integer In ItemList
- If DiscreteDistrDict.ContainsKey(s) Then
- DiscreteDistrDict(s) += 1
- Else
- DiscreteDistrDict.Add(s, 1)
- End If
- Next
- End Sub
- Private Sub PrintDiscreteDistribution(Dict As SortedDictionary(Of Integer, Integer))
- Me.RichTextBox1.AppendText(vbCrLf & "DISCRETE DISTRIBUTION:" & vbCrLf _
- & "VALUE".PadRight(8) & "COUNT".PadRight(8) _
- & "FREQ.".PadRight(8) & "PERC.".PadRight(8) & vbCrLf)
- For Each s As KeyValuePair(Of Integer, Integer) In Dict
- Me.RichTextBox1.AppendText(s.Key.ToString.PadRight(8) & s.Value.ToString.PadRight(8) _
- & (s.Value / NumOfElements).ToString().PadRight(8) _
- & ((s.Value / NumOfElements * 100).ToString & "%").PadRight(8) _
- & vbCrLf)
- Next
- End Sub
- Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
- Me.RichTextBox1.ScrollToCaret()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement