Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > Visual Basic 2010 General Discussion
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 6th, 2010, 12:46 PM
Authorized User
 
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Send a message via MSN to SamC
Default Calculating the Modal Average of an Array of Numbers

OK, so I've been given an assignment due for Monday, which was to create a console program which allows the user to enter an unknown quantity of numbers, then have the Mean, Median and Mode averages displayed of said group of numbers.

So far I've managed to get the Mean and Median to work, for both odd and even quantities of variables. I am however stuck on getting it to calculate the modal average! I don't really know how to approach this, would a structure with two fields, variable and quantity of variable, work?

Below is the code, I haven't made it display discrete of continuous data yet, I'm leaving that for the end.

Code:
Module Module1

    Sub Main()
Start:
        'Declare Variables
        Dim dataType As String = "x"
        Dim userInput As String = "x"
        Dim data(0) As Double
        Dim sum, mean, median, mode As Double
        Dim count As UInt32 = 0
        Dim n As UInt32 = 0

        'Reset Values
        sum = 0 : count = 0 : mean = 0 : median = 0 : mode = 0

        'Set Title
        Console.Title = "Statisticulator - By Sam Christy"

        'Determine data type from user input
        Do
            Console.WriteLine("Is your Continuous (C) or Data Discrete (D)?")
            dataType = Console.ReadLine()

            If LCase(dataType) = "d" Then
                dataType = "Discrete"
            ElseIf LCase(dataType) = "c" Then
                dataType = "Continuous"
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Invalid input: Please enter either 'C' or 'D'" & vbNewLine)
                Console.ResetColor()
            End If

        Loop Until dataType = "Discrete" Or dataType = "Continuous"

        Console.Clear()
        Console.WriteLine("Data Type: {0}{1} ", dataType, vbNewLine)

        'Allow user to input data and populate the array providing data is valid

        Do
            Console.Write("Number{0}: ", n + 1)
            userInput = Console.ReadLine()
            If userInput = "" Then
                Exit Do
            Else
                ReDim Preserve data(n)
                data(n) = userInput
                n += 1
            End If
        Loop

        'Calculate Sum of Variables
        For Each number In data
            sum += number
        Next
        Console.Clear()

        'Sort the array of Variables in ascending order and set value of count
        SortData(data)
        count = data.Count

        'Calculate Mean
        CalculateMean(data.Count, sum, mean)

        'Calculate Median
        CalculateMedian(data, count, median)

        'Calculate Mode
        PrintData(data)

        'Display Results
        Console.WriteLine("")
        Console.WriteLine("You entered {0} variables", count)
        Console.WriteLine("Sum: {0}", sum)
        Console.WriteLine("Mean: {0}", mean)
        Console.WriteLine("Median: {0}", median)
        Console.WriteLine("Mode: {0}", mode)
        Console.WriteLine("")
        Console.WriteLine("")
        GoTo Start

    End Sub

    Sub CalculateMean(ByVal count As UInt32, ByVal sum As Double, ByRef mean As Double)
        mean = sum / count
    End Sub

    Sub CalculateMedian(ByVal data As Array, ByVal count As UInt32, ByRef median As Double)
        If count Mod 2 <> 0 Then
            median = data(((count + 1) / 2) - 1)
        Else
            median = (data((count - 2) / 2) + data(count / 2)) / 2
        End If
    End Sub

    Sub PrintData(ByVal data As Array)
        For Each v In data
            Console.WriteLine(v)
        Next
    End Sub

    Sub SortData(ByRef ary As Array)
        Dim Outer, Inner As Integer
        Dim Temp As Double
        For Outer = UBound(ary) To 0 Step -1
            For Inner = 0 To Outer - 1
                If ary(Inner) > ary(Inner + 1) Then
                    Temp = ary(Inner)
                    ary(Inner) = ary(Inner + 1)
                    ary(Inner + 1) = Temp
                End If
            Next
        Next
    End Sub

End Module
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
 
Old November 7th, 2010, 05:52 AM
Authorized User
 
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Send a message via MSN to SamC
Exclamation

Sorry for double posting, but I really need some help here!
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein

Last edited by SamC; November 7th, 2010 at 06:16 AM..
 
Old November 7th, 2010, 07:17 PM
Authorized User
 
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Send a message via MSN to SamC
Default

It's OK, I found some code online and implemented it in my program, although I don't fully understand it!
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein





Similar Threads
Thread Thread Starter Forum Replies Last Post
Modal Popup Extender - not modal? jenbuh ASP.NET 3.5 Professionals 5 July 27th, 2009 07:04 PM
How to get least value numbers in a sorted array? ashokparchuri Other Programming Languages 3 December 5th, 2006 09:25 AM
Calculating numbers of days similar to Excel klott16 Access 7 January 3rd, 2006 02:43 PM
fillin array with letters from a to Z and numbers sajid C# 10 May 3rd, 2005 03:38 PM
Find average for values stored in an array ja8261 VB.NET 2002/2003 Basics 2 October 29th, 2004 10:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.