I need to create a program to find the mean, mode, meadian, and range. I have the mean so far. I think I can use the SelectionSort to find the range but not sure. Can anyone help me with this? I would like to assign it to my class as a project but wanted to practice it before assigning. New Teacher new to VB.
vbamatuer wrote:> I need to create a program to find the mean, mode, meadian, and range. I> have the mean so far. I think I can use the SelectionSort to find
range but not sure. Can anyone help me with this? I would like to
assign> it to my class as a project but wanted to practice it before assigning.> New Teacher new to VB.
OK, I am not clear on the definitions so I will make this a general comment.
Assume you start with an array of values or a listbox .
You know how many elements in the array.
the average (i think this is arithmentic mean) is the addition of all values divided by the number of values.
the range is lowest to highest value. read through the array testing each value against a stored high and low value. assume array of 100 elements with index 0 to 99
maxval = arr(0) ' set high and low value minval = arr(0) for inx = 1 to 99 if arr(inx) > maxval then maxval = arr(inx) if arr(inx) < minval then minval = arr(inx) next x
range = maxval - minval
------------------- the other value is where there are an equal number of values higher and lower. simple way of doing this is to put the values into a listbox with sort = true. for 100 elements you would select element 49 or 50 (adjustment for duplicate values) -------------------
Public Class Form1 Dim NumList() As Integer Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblMean.Click
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click RandomArray() DisplayArray() DisplayCount() Sum() Min() Max() Mean() Range() Median() Mode() End Sub
Private Sub RandomArray() Dim num, i, length As Integer 'declair num as and integer'
length = CInt(txtCount.Text) - 1 For i = 0 To length ReDim Preserve NumList(i)
num = (100 * Rnd()) NumList(i) = num
Next
End Sub Private Sub DisplayArray() txtDisplay.Text = "" For i = 0 To (NumList.Length - 1) txtDisplay.Text = txtDisplay.Text & NumList(i).ToString & ","
Next
End Sub Private Sub DisplayCount()
txtCount.Text = NumList.Length
End Sub Private Sub Sum() Dim Total = 0
For i = 0 To (NumList.Length - 1) Total = Total + NumList(i)
Next txtSum.Text = Total.ToString
End Sub Private Sub Min() 'minimum number in the list' Dim min As Integer
min = NumList(0) For i = 0 To (NumList.Length - 1) If NumList(i) < min Then min = NumList(i)
End If Next txtMin.Text = min.ToString
End Sub Private Sub Max() 'maxium nuber in the list' Dim max As Integer
max = NumList(0) For i = 0 To (NumList.Length - 1) If NumList(i) > max Then max = NumList(i)
End If Next txtMax.Text = max.ToString
End Sub Private Sub Mean() ' sum divided by the count' Dim Total As Integer
Total = txtSum.Text txtMean.Text = txtSum.Text / txtCount.Text
End Sub Private Sub Range() ' max - min' Dim Range As Integer
Range = txtMax.Text - txtMin.Text txtRange.Text = Range.ToString
End Sub Private Sub Median() Dim temp1, temp2, changed, middle As Integer
changed = 1
Do While changed <> 0 changed = 0 For i = 0 To (NumList.Length - 2) temp1 = NumList(i) temp2 = NumList(i + 1) If temp1 > temp2 Then NumList(i) = temp2 NumList(i + 1) = temp1 changed = 1 End If Next Loop middle = NumList.Length \ 2 If (NumList.Length Mod 2) = 0 Then temp1 = NumList(middle) temp2 = NumList(middle - 1) txtMedian.Text = (temp1 + temp2) / 2
Else txtMedian.Text = (NumList(middle)).ToString End If End Sub
If you would like to report an abuse of our service, such as a spam message, please . Если Вы хотите пожаловаться на содержимое этой страницы, пожалуйста .