Results 1 to 8 of 8

Thread: [RESOLVED] Standard Deviation Issues

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Resolved [RESOLVED] Standard Deviation Issues

    Alright so im making a program that reads a text file that lists a persons name then next line has the persons test score. I am trying to calculate the standard deviation for any given amount of people. I got the mean calculation to work, but I can't seem to see why my standard deviation calculation doesnt work. Please check it out for me!

    Code:
    Dim name As String, total, x, n as Double
    Dim sr As IO.StreamReader = IO.File.OpenText("scores.txt")
    Dim count As Double = 0
    Do While sr.peek <> -1
         name = sr.ReadLine
         total += CDbl(sr.ReadLine)
         count += 1
    Loop
    Do While sr.Peek <> -1
         name = sr.ReadLine
         x = CDbl(sr.ReadLine)
         n += ((x - total) ^ 2)
    Loop
    ListBoxdata.items.add("Mean = " & Math.Round(total/count, 1))
    ListBoxdata.items.add("Standard Deviation = " & Math.Sqrt(n / count))
    end sub
    Standard Deviation Formula = http://en.wikipedia.org/wiki/Standard_deviation
    Last edited by mxzx440; Nov 17th, 2009 at 11:31 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Standard Deviation Issues

    try this:

    vb Code:
    1. x = CDbl(sr.ReadLine)
    2. n += ((x - total) ^ 2)

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Standard Deviation Issues

    actually, you need to put this before the 2nd do loop:

    vb Code:
    1. sr.dispose
    2. sr = IO.File.OpenText("scores.txt")

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Standard Deviation Issues

    The first post you suggested is the same thing I have?

    I added the second post and that at least got me a number! But its the wrong number, its 360...

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Standard Deviation Issues

    Quote Originally Posted by mxzx440 View Post
    The first post you suggested is the same thing I have?
    you edited your typo


    how far out is it?

    try using decimals instead of doubles

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Standard Deviation Issues

    Yeah, I noticed I had typed a _ instead of a ) when I looked at my post after I had already replied...haha wasn't trying to undermine you. Anyway, it is suppose to equal 8.9 with my data. Before it does the square root and dividing by 5, it should equal 390 something. So it is doing just about everything wrong...

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Standard Deviation Issues

    this might be the problem:

    Code:
    Do While sr.Peek <> -1
         name = sr.ReadLine
         x = CDbl(sr.ReadLine)
         n += ((x - total) ^ 2)
    Loop

    compute the difference of each data point from the mean, and square the result

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Standard Deviation Issues

    OH MY GOODNESS! That was it! Thank you soooooo much! I am going to rate you for sure! I knew a fresh set of eyes would help, so glad it was an easy error. I need to stop working on this so late at night...

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