|
-
Sep 30th, 2015, 08:17 PM
#5
New Member
Re: VS2010 Finding the mode of a set of user inputted integers
 Originally Posted by .paul.
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
I know this is a SUPER dead thread and i apologize for bringing it back, But I'm doing this project by myself (Its not homework, I just have a statistics class and I want to build my own calculator for fun because I do enjoy learning programming) However, I copy Pasta'd your code (Not that I wanted to but because I tried doing it by myself and It wasnt working, so i wanted to test yours) and Yours didnt work either :/
Dim values() As String = ListBox1.Items.Cast(Of String).OrderBy(Function(s) CDec(s)).ToArray
Gives this error: "Unable to cast object of type 'System.Int32' to type 'System.String'.
Any reason why this would do this that you can think of? I've been wracking my brain with this for 3 days now and i cant seem to pinpoint the issue.
Thanks in advance and Sorry to bring this dead thread alive again ;~;
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|