Results 1 to 3 of 3

Thread: Contest 10: Mean, Median, and Mode - Peter Porter

  1. #1

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    538

    Contest 10: Mean, Median, and Mode - Peter Porter

    Took a little head banging, just hope my app isn't overkill.

    Development info:
    Visual Basic 2010 Express
    .NET Framework 4.0
    Windows 7
    64bit app

    Code:
    Module Module1
    
        Dim stp As New Stopwatch
        Dim intro As Integer = 0
    
        'contest info
        Sub Info()
            Dim info2 As String = " Contest 10: Mean, Median, and Mode - Peter Porter"
            Dim info3 As String = " 21 Dec 2015 - 1 Jan 2016"
            Dim info4 As String = "*** Entered numbers must be comma or space separated. ***"
            Console.SetCursorPosition((Console.WindowWidth - info2.Length) / 2, Console.CursorTop)
            Console.WriteLine(info2)
            Console.WriteLine()
            Console.SetCursorPosition((Console.WindowWidth - info3.Length) / 2, Console.CursorTop)
            Console.WriteLine(info3)
            Console.WriteLine()
            Console.SetCursorPosition((Console.WindowWidth - info4.Length) / 2, Console.CursorTop)
            Console.WriteLine(info4)
        End Sub
    
    
    
        Sub Main()
    
            stp.Reset()
    
            'call to info sub
            If intro = 1 Then
            Else
                Info()
                intro = 1
            End If
    
            Dim sum As Double = 0
            Dim int2 As Double()
            Dim int3 As Double()
            Dim sorted As String = ""
    
            'input numbers
            Console.WriteLine() : Console.WriteLine()
            Console.Write(" Enter numbers: ")
    
    
            'reads inputted data
            Dim numbers As String = Console.ReadLine()
    
            'creates string arrays from numbers
            Dim separators() As String = {"*", ",", " "}
            Dim input As String() = numbers.Split(separators, StringSplitOptions.RemoveEmptyEntries)
    
            'checks for non-numerical data entered
            'if found, resets app for re-entering numerals
            For i4 = 0 To input.Length - 1
                int3 = New Double(input.Length - 1) {}
                Dim isNumerical As Boolean = Integer.TryParse(input(i4), int3(i4))
                If isNumerical = False Then
                    Console.WriteLine() : Console.WriteLine()
                    Console.WriteLine(" *** Huh? Please enter numbers. ***")
                    Console.WriteLine()
                    Console.WriteLine(" Example: 23, 19, 36, 40, 28, 42, 15")
                    Console.WriteLine() : Console.WriteLine()
                    Main()
                Else
                End If
            Next
    
            'verifies that at least 6 numbers have been entered
            For i = 0 To 5
                If input.ElementAtOrDefault(i) Is Nothing Then
                    Console.WriteLine() : Console.WriteLine()
                    Console.WriteLine(" *** Not Enough. Enter at least 6 numbers. ***")
                    Console.WriteLine()
                    Console.Write(" Example: 23, 19, 36, 40, 28, 42, 15")
                    Console.Write("   Yes, the example shows 7. Sheesh!")
                    Console.WriteLine() : Console.WriteLine()
                    Main()
                Else
                End If
            Next
    
    
            stp.Start()
    
    
            For i As Double = 0 To input.Length - 1
    
                'total of all numbers, divided later by input length to get the Mean
                sum = sum + CInt(input(i))
    
                'lists all numbers into an array
                int2 = New Double(input.Length - 1) {}
                For i3 As Integer = 0 To input.Length - 1
                    Dim isNumerical As Boolean = Integer.TryParse(input(i3), int2(i3))
                    If isNumerical = False Then
                    Else
                    End If
                Next
    
                'sort numbers
                Array.Sort(int2)
                sorted = String.Join(", ", int2)
            Next
    
    
            'gets mean
            Dim mean As Double = 0
            mean = sum / input.Length
    
    
            'finds median
            Dim median As Double
            If (UBound(int2) Mod 2) = 0 Then
                median = int2(UBound(int2) \ 2)
            Else
                median = (int2(UBound(int2) \ 2) + int2(1 + UBound(int2) \ 2)) / 2
            End If
    
            'displays sorted numbers, total, mean and median
            Console.WriteLine() : Console.WriteLine() : Console.WriteLine()
            Console.WriteLine("       Sorted:  {0}", sorted)
            Console.WriteLine()
            Console.WriteLine("        Total:  {0}", sum)
            Console.WriteLine()
            Console.WriteLine()
            Console.WriteLine("         Mean:  {0}", mean)
            Console.WriteLine()
            Console.WriteLine("       Median:  {0}", median)
            Console.WriteLine()
    
            'tallys occurrences, for finding mode later
            Dim values As New Dictionary(Of Integer, Integer)()
            For Each value2 In input
                If Not values.ContainsKey(value2) Then
                    values.Add(value2, 0)
                End If
                values(value2) += 1
            Next
    
            Dim comp As Integer = 0       'for highest occurrence to be compared against
            Dim i5 As Integer = 0         'for number of modes
    
            'finds mode, if any
            For Each value In values.Keys
                For Each value2 In values.Keys
                    'finds highest occurrence
                    If values(value2) > comp Then
                        comp = values(value2)
                    End If
                Next
    
                'prevents lowest occurrence from being displayed
                If values(value) = 1 Then
                Else
                    'ensures highest occurrence is displayed
                    If values(value) < comp Then
                    Else
                        'displays mode
                        If i5 = 0 Then
                            Console.Write("         Mode:  {0} ", value, values(value))
                        Else
                            Console.Write(", {0} ", value, values(value))
                        End If
                        i5 = i5 + 1
                    End If
                End If
            Next
    
            'if no mode
            If comp = 1 Then
                Console.Write("         Mode:  None")
            Else
            End If
    
            'displays process time
            stp.Stop()
            Console.WriteLine() : Console.WriteLine() : Console.WriteLine()
            Console.Write(" Process time in milliseconds:  ") : Console.Write(stp.Elapsed.ToString("ss\.fff"))
    
            'try again?
            Console.WriteLine() : Console.WriteLine() : Console.WriteLine()
            Console.Write(" Again? (Y/N) ")
            Dim yn As String = Console.ReadLine()
            Try
                If String.Compare(yn, "y", True) = 0 Or String.Compare(yn, "yes", True) = 0 Then
                    Console.Clear()
                    Main()
                Else
    
                End If
            Catch ex As Exception
            End Try
    
        End Sub
    
    End Module
    Attached Files Attached Files
    Last edited by Peter Porter; Feb 3rd, 2016 at 09:50 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Contest 10: Mean, Median, and Mode - Peter Porter

    I received Peter Porter's entry and it mostly works. The code is lengthy. It does not account for Doubles(stated in rules) and does not provide the mode if there is more than 1(possible ambiguity in rules). I do like how it times the execution and gives the user clear instructions.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    538

    Re: Contest 10: Mean, Median, and Mode - Peter Porter

    Yeah, it just works with regular numbers with no decimals, correctly identifying modes if there are any.

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
  •  



Click Here to Expand Forum to Full Width