Solved

Mean, Mode , Median fuctions

Posted on 2004-10-11
11
1,449 Views
Last Modified: 2013-12-26
I found a program at a vb website that did several statisical calculations including Mean(average), Mode and median.  I am tring to make a simple program for my kids to check their homework.  I have a form with a textbox to enter a set of numbers, which then are added to listbox(one at a time) and once the user clicks the calculate button in three different textboxes are displayed the Mean the(the average of the numbers) and the mode ( the number that appears the most), and in the last textbox the median the number that is the middle number of the set.

example:  10, 10, 15, 17, 20, 25

Mean = 16.16            10 +10+15+ 17+20+25=  97/ 6 = 16.16
Mode=  10                 10 , 10 (2 10's)
Median = 16               10, 10, 15  (16) 17, 20 ,25 on even numbers you take the 2 middle number, add them together then divide by 2, odd set of numbers it is the middle number.

Is there any built in functions  for these, or how would I go about in creating them.

Thanks.........
0
Comment
Question by:ucla11
  • 5
  • 4
  • 2
11 Comments
 
LVL 11

Expert Comment

by:bingie
ID: 12283109
Try these for the mean and the median, just pass the name of the filled listbox to the function:

Private Function median(lstBox As Control)
If lstBox.ListCount Mod 2 = 1 Then
    lstBox.ListIndex = (lstBox.ListCount / 2) - 0.5
    median = lstBox.Text
    Exit Function
Else
    lstBox.ListIndex = (lstBox.ListCount / 2) - 1
    sum = Val(lstBox.Text)
    lstBox.ListIndex = lstBox.ListIndex + 1
    sum = sum + Val(lstBox.Text)
    median = sum / 2
End If
End Function

Private Function mean(lstBox As Control)
Dim sum As Long

For i = 0 To lstBox.ListCount - 1
 lstBox.ListIndex = i
 sum = sum + Val(lstBox.Text)
Next
 mean = sum / i
End Function

'Call it a-la

myMean = mean(List1)
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12283173
0
 
LVL 11

Accepted Solution

by:
bingie earned 500 total points
ID: 12283231
My original post, cleaned up a bit. Check out the second of Mikal613s link, it has a good function for Mode.

Private Function median(lstbox As Control)
Dim sum As Long

If lstbox.ListCount Mod 2 = 1 Then
    lstbox.ListIndex = (lstbox.ListCount / 2) - 0.5
    median = lstbox.Text
Else
    x = (lstbox.ListCount / 2)
    sum = Val(lstbox.List(x - 1)) + Val(lstbox.List(x))
    median = sum / 2
End If
End Function


Private Function mean(lstbox As Control)
Dim sum As Long

For i = 0 To lstbox.ListCount - 1
 lstbox.ListIndex = i
 sum = sum + Val(lstbox.Text)
Next
 average = sum / i
End Function
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:ucla11
ID: 12285094
bingie , I like your code, do have a similair one for Mode?  also what if there is more than one number as mode
example:  10, 10, 12, 12, 23, 33,   would it show 10 and 12 as the mode?
0
 

Author Comment

by:ucla11
ID: 12285108
Also how would I apply these functions to the listbox numbers.........  Mean(lstBox1.Text) ?....Mean(lstBox1.List) ?
0
 
LVL 11

Expert Comment

by:bingie
ID: 12286232
You would pass the name of the listbox to the function, you can then return the value to a variable or a msgbox, etc.

MsgBox Mean(List1) 'Change List1 to the name of your list box
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12286262
Private sub MyMsgBox(Lst as listbox)
  msgbox mean(lst.list)

end sub
0
 

Author Comment

by:ucla11
ID: 12289604
I tryed your code but the mean does not show anything in the textbox as a result, the meian works fine. Does anyone have an idea how to do the mode function?

Here is what I have now.....
Text2.Text is for the Mediam'  Text3.Text is for Mean, and Text4.Text is for the Mode(do not have code yet!)

Private Function median(lstbox As Control)
Dim sum As Long

If lstbox.ListCount Mod 2 = 1 Then
    lstbox.ListIndex = (lstbox.ListCount / 2) - 0.5
    median = lstbox.Text
Else
    x = (lstbox.ListCount / 2)
    sum = Val(lstbox.List(x - 1)) + Val(lstbox.List(x))
    median = sum / 2
End If
End Function

Private Function mean(lstbox As Control)
Dim sum As Long
Dim average As Long
Dim i As Integer
For i = 0 To lstbox.ListCount - 1
 lstbox.ListIndex = i
 sum = sum + Val(lstbox.Text)
Next
 average = sum / i
End Function

Private Sub Command1_Click()
lstbox.AddItem (CLng(Text1.Text))
Text1.Text = ""
Text1.SetFocus
End Sub

Private Sub Command2_Click()
Text2.Text = median(lstbox)
Text3.Text = mean(lstbox)
End Sub

Private Sub Command4_Click()
End
End Sub
0
 
LVL 11

Expert Comment

by:bingie
ID: 12290447
Aww shoot - I have to stop posting at 2:00 am :)

Change the line:

average = sum / i
  to
mean = sum / i
0
 

Author Comment

by:ucla11
ID: 12292980
I still a a way to figure out the Mode......anybody?
0
 
LVL 11

Expert Comment

by:bingie
ID: 12302883
Check out this code, contains functions for most statistical applications.

http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=21647&lngWId=1
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 74
message box in access 4 51
Add a task in Outlook from access 11 42
How to Add / Edit Windows Menu 4 68
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question