this might help. i used a listbox, a textbox, + 3 buttons to calculate mean, median, + mode of a set of user entered numbers:
Code:Public Class Form1 Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then Dim value As Decimal If Decimal.TryParse(TextBox1.Text, value) Then ListBox1.Items.Add(value.ToString) End If TextBox1.Clear() End If End Sub Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick Dim index As Integer = ListBox1.IndexFromPoint(e.Location) If index <> -1 Then ListBox1.Items.RemoveAt(index) End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'mean MsgBox(ListBox1.Items.Cast(Of String).Average(Function(s) CDec(s))) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'median Dim values() As String = ListBox1.Items.Cast(Of String).OrderBy(Function(s) CDec(s)).ToArray If ListBox1.Items.Count Mod 2 = 1 Then 'odd count MsgBox(values(CInt(Math.Floor(ListBox1.Items.Count / 2)))) Else 'even count MsgBox(String.Format("{0}, {1}", values(ListBox1.Items.Count \ 2 - 1), values(ListBox1.Items.Count \ 2))) End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 'mode Dim values() As String = ListBox1.Items.Cast(Of String).OrderBy(Function(s) CDec(s)).ToArray Dim occurences() As Integer = Array.ConvertAll(values, Function(s1) values.Count(Function(s2) s2 = s1)) Dim done As New List(Of String) Dim output As String = "" For x As Integer = 0 To occurences.GetUpperBound(0) If occurences(x) = occurences.Max AndAlso Not done.contains(values(x)) Then done.Add(values(x)) output &= values(x) & ", " End If Next MsgBox(String.Format("{0}{1}{2}", If(done.Count = 1, "", If(done.Count = 2, "BiModal", "MultiModal")), Environment.NewLine, output.TrimEnd(","c, " "c))) End Sub End Class




Reply With Quote
