HELP PLEASE!!!

I'm trying to do the standard deviation for a list of numbers using 'For' 'Next' 'Return' and I can't seem to get it right. Here is my code so far:

Dim upperBound As Integer
Dim grade, mean, score, title As String


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


End Sub

Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn.Click
Dim m, sd, sx As Integer
Dim scores() As Integer = {0, 59, 60, 65, 75, 56, 90, 66, 62, 98, 72, 95, 71, 63, 77, 65, 77, 65, 50, 85, 62}
LBx.Items.Add(sum(scores) / 20)
Dim fmtstr As String = "{0,-5} {1,20}"
LBx.Items.Add(stdev(scores) / 20)

End Sub

Function sum(ByVal s() As Integer) As Integer
'Set up mean calculation
Dim i, m As Integer
m = 0
For i = 1 To s.GetUpperBound(0)
m += s(i)
Next
Return m
End Function

Function stdev(ByVal s() As Integer) As Integer
'Set up standard deviation
Dim i, m, sx As Integer
sx = 0
For i = 1 To s.GetUpperBound(0)
sx += ((s(i) - m) ^ 2)
Next
Return sx
End Function
End Class

The Standard deviation is suppose to be 13.06. I'm not sure what to do next. Thanks a lot!

Michael

Comments

  • [blue]I haven't done with .NET but I suspect a rounding issue with the integer values. I would change to something with greater precsion.


    jim b

    [/blue]
  • : I'm trying to do the standard deviation for a list of numbers using 'For' 'Next' 'Return' and I can't seem to get it right. Here is my code so far:
    :
    : Dim upperBound As Integer
    : Dim grade, mean, score, title As String
    :
    :
    : Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    :
    :
    : End Sub
    :
    : Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn.Click
    : Dim m, sd, sx As Integer
    : Dim scores() As Integer = {0, 59, 60, 65, 75, 56, 90, 66, 62, 98, 72, 95, 71, 63, 77, 65, 77, 65, 50, 85, 62}
    : LBx.Items.Add(sum(scores) / 20)
    : Dim fmtstr As String = "{0,-5} {1,20}"
    : LBx.Items.Add(stdev(scores) / 20)
    :
    : End Sub
    :
    : Function sum(ByVal s() As Integer) As Integer
    : 'Set up mean calculation
    : Dim i, m As Integer
    : m = 0
    : For i = 1 To s.GetUpperBound(0)
    : m += s(i)
    : Next
    : Return m
    : End Function
    :
    : Function stdev(ByVal s() As Integer) As Integer
    : 'Set up standard deviation
    : Dim i, m, sx As Integer
    : sx = 0
    : For i = 1 To s.GetUpperBound(0)
    : sx += ((s(i) - m) ^ 2)
    : Next
    : Return sx
    : End Function
    : End Class
    :
    : The Standard deviation is suppose to be 13.06. I'm not sure what to do next. Thanks a lot!
    :
    : Michael
    :

    I made changes to the stddev function ...

    Function stdev(ByVal s() As Integer) As Double
    'Set up standard deviation
    Dim i, m As Integer
    Dim sx As Double

    ' NOTE m IS NOT BEING SET IN YOUR FUNTION

    sx = 0
    For i = 1 To s.GetUpperBound(0)
    sx += ((s(i) - m) ^ 2)
    Next

    Return sx
    End Function

    note that the return type is a double to preserve the decimal ... also i changed the "sx" variable to a double as well ... to prevent these kinds of conversion errors you should turn Option Strict on at all times.

    Finally I do not know the formula for standard deviation but I do know the the "m" variable in your function isn't doing anything.

    Hope this gets you back on track ...

  • Here was my final code: I'm still trying to figure out how to round decimals. I know how(i think), just don't know where to place code.

    ________________________________________________________________________
    Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn.Click
    Dim i, a, b, c, d, f As Integer
    Dim m, sd, sx, s As Integer
    Dim scores() As Integer = {0, 59, 60, 65, 75, 56, 90, 66, 62, 98, 72, 95, 71, 63, 77, 65, 77, 65, 50, 85, 62}
    Dim grade, first As String
    Dim fmtstr As String = "{0,-5}, {1,20}"
    Dim mean, score As String
    first = "There were 20 exams"
    LBx.Items.Clear()
    LBx.Items.Add(first)
    fmtstr = "{0,5}, {1,24}"
    LBx.Items.Add(String.Format(fmtstr, "Mean:", (sum(scores) / 20)))
    fmtstr = "{0,5}, {1,20}"
    LBx.Items.Add(String.Format(fmtstr, "Std. Deviation:", (Math.Sqrt(stdev(scores) / 19))))


    LBx.Items.Add(String.Format(fmtstr, "Score", "Grade"))

    For i = 1 To 20
    m = 70.65
    sx = 13.06
    If scores(i) >= (m + (1.5 * sx)) Then grade = "A" Else
    If (m + (0.5 * sx)) <= scores(i) And scores(i) < (m + (1.5 * sx)) Then grade = "B" Else
    If (m - (0.5 * sx)) <= scores(i) And scores(i) < (m + (0.5 * sx)) Then grade = "C" Else
    If (m - (1.5 * sx)) <= scores(i) And scores(i) < (m - (0.5 * sx)) Then grade = "D" Else
    If scores(i) < m - (1.5 * sx) Then grade = "F"
    LBx.Items.Add(String.Format(fmtstr, scores(i), grade))
    grade = ""
    Next i
    End Sub


    Function sum(ByVal s() As Integer) As Integer
    'Set up mean calculation
    Dim i, m As Integer
    m = 0
    For i = 1 To s.GetUpperBound(0)
    m += s(i)
    Next
    Return m
    End Function

    Function stdev(ByVal s() As Integer) As Double
    'Set up standard deviation
    Dim i, m As Integer
    Dim sx As Double
    m = 70.65
    For i = 1 To s.GetUpperBound(0)
    sx += (s(i) - m) ^ 2
    Next
    Return sx

    End Function

    End Class

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories