Results 1 to 13 of 13

Thread: Help with arrays

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    10

    Help with arrays

    Hey guys, brand new to this forum. I'm not sure if I'm posting in the right place.
    I have looked in my textbook and searched online and cannot find anything that fits my situation. I know this is easier than I'm making it, but I'm new to coding and really need some help. For this project I am supposed to put theses particular values into an array {scores(19)}. Then I am supposed to write the program to find the mean and standard deviation of the values in the array. Finding the mean was easy. However, the standard deviation is a little more difficult. The formula is as follows:

    SD = math.sqrt((x(1) - m)^2 + (x(2) - m)^2)...(x(20) - m)^2)/n)

    where x is the array, (1,2...etc) is the element in the array, m is the mean and n is the total number of elements in the array.

    My problem is figuring out how to tell VB to handle each element separately. I have no problem writing a loop to add the elements together, but I'm having big issues using each element as a standalone value.

    Please help, please let me know if this is answered somewhere else, and please let me know if I need to clear this up. This project is due monday, November 30, 2009 and school is closed until then for the holidays.

    Thanks for the help guys,
    Erexshin

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Help with arrays

    Quote Originally Posted by Erexshin View Post
    ..I'm having big issues using each element as a standalone value...
    What do you mean? you can use the elements in the array as you did in the formula: x(1) is element 1 in the array, x(2) is element 2 in the array (this assuming you loaded your Array starting from element 1, you could start at 0 since arrays are 0 based)

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    10

    Re: Help with arrays

    That is one way to do it. But, let's say this array was dynamic, and grew to have more than 20 elements; that formula would no longer work as it would only find the standard deviation of the first 20 elements. How would I write it to go from Lbound to Ubound and plug each element into the formula?

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Help with arrays

    Ok, i understand, see this code, it fires with a CommandButton..
    Code:
    Private Const Items As Long = 19
    Private x(Items) As Long
    
    Private Sub Command1_Click()
    Dim Mean As Long, SD As Long
    
        LoadArray
        Mean = CalculateMean
        SD = CalculateStandardDeviation(Mean)
        
        MsgBox SD
    End Sub
    
    Private Sub LoadArray()
    Dim i As Long
    
        For i = 0 To UBound(x)
            x(i) = i 'The  Array will be loaded with numbers from 0 to 19
        Next
    End Sub
    
    Private Function CalculateMean() As Long ' Returns Mean
    Dim i As Long, AuxVar As Long
    
        For i = 0 To UBound(x)
            AuxVar = AuxVar + x(i)
        Next
        AuxVar = AuxVar / Items
        CalculateMean = AuxVar
    End Function
    
    
    Private Function CalculateStandardDeviation(m As Long) As Long  ' Returns SD
    Dim i As Long, AuxVar As Long, SD As Long
    
        For i = 0 To UBound(x)
            AuxVar = (x(i) - m) ^ 2
            SD = SD + AuxVar
        Next
        SD = Sqr(SD / Items)
        CalculateStandardDeviation = SD
    End Function
    20 items in an array that go from 0 to 19. I'm not sure if the SD is being calculated correctly, but at least you can get the idea on how to do it. Tell me if it works.
    Last edited by jcis; Nov 25th, 2009 at 03:17 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    10

    Re: Help with arrays

    Thank you for the quick replies. I will try this out first thing in the morning. Gotta get some sleep. Thanks again and I will definitely post tomorrow regarding this.

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Help with arrays

    Your formula for calculating standard deviation is wrong; you're supposed to divide the total of the squares by the number of elements (minus one) before you get the square root.

    These functions will take any numeric array (Integer, Long, Currency, Single, Double) and return the mean and stddev as doubles.
    vb Code:
    1. Public Function Mean(pvarArray As Variant) As Double
    2.     Dim lngCount As Long
    3.     Dim dblTotal As Double
    4.     Dim i As Long
    5.  
    6.     If Not IsArray(pvarArray) Then Exit Function
    7.     For i = LBound(pvarArray) To UBound(pvarArray)
    8.         lngCount = lngCount + 1
    9.         dblTotal = dblTotal + pvarArray(i)
    10.     Next
    11.     Mean = dblTotal / CDbl(lngCount)
    12. End Function
    13.  
    14. Public Function StdDev(pvarArray As Variant, pdblMean As Double) As Double
    15.     Dim lngCount As Long
    16.     Dim dblTotal As Double
    17.     Dim i As Long
    18.  
    19.     If Not IsArray(pvarArray) Then Exit Function
    20.     For i = LBound(pvarArray) To UBound(pvarArray)
    21.         lngCount = lngCount + 1
    22.         dblTotal = dblTotal + (pvarArray(i) - pdblMean) ^ 2
    23.     Next
    24.     If lngCount > 1 Then dblTotal = dblTotal / CDbl(lngCount - 1)
    25.     StdDev = Sqr(dblTotal)
    26. End Function
    Sample usage:
    Code:
    Private Sub Command1_Click()
        Dim lngArray(20) As Long
        Dim dblMean As Double
        Dim dblStdDev As Double
        Dim i As Long
    
        ' Randomize should only go in your project's entrypoint (Sub Main or Form_Load)
        Randomize
        ' Populate array with random values (0-999)
        For i = LBound(lngArray) To UBound(lngArray)
            lngArray(i) = Int(1000 * Rnd)
        Next
        ' Get mean and stddev
        dblMean = Mean(lngArray)
        dblStdDev = StdDev(lngArray, dblMean)
        ' Display results
        MsgBox "Mean: " & Format(dblMean, "0.000") & vbNewLine & "StdDev: " & Format(dblStdDev, "0.000")
    End Sub

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    10

    Re: Help with arrays

    Just an update. Still working the kinks out on this, and I looked in my textbook; this stuff is found nowhere in the book. It's a good thing I paid $300 for this book.

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Help with arrays

    Quote Originally Posted by Erexshin View Post
    Just an update. Still working the kinks out on this, and I looked in my textbook; this stuff is found nowhere in the book. It's a good thing I paid $300 for this book.
    It's normal having some doubts at the begining, i think now you have enough code to find the solution by yourself, if you can do it that would be the best, it's always better when you write and understand you're own code. I see you bought a book, that's really good, also use google and the forums when looking for info about specific topics, keep pushing and you'll get it.

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Help with arrays

    What kinks?

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    10

    Re: Help with arrays

    OK. Thank you all for your help. You guys definitely got me pointed in the right direction. this is my first class in my major and we are only covering the basics of VB. A lot of the things you were showing me was out of our range, so to speak. But, it did help me see what the program was supposed to be doing. All in all, using what we have been taught, this is what I came up with:


    vb Code:
    1. Dim scores() As Double = {59, 60, 65, 75, 56, 90, 66, 62, 98, 72, 95, 71, 63, 77, 65, 77, 65, 50, 85, 62} 'Places the scores into the array
    2.     Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    3.         Dim add, n, mean, sd As Double 'This declares which variables are to be used as a double data type
    4.         Dim grade As String 'This declares which variables are to be used as a string data type
    5.         add = 0 'assigns a base value of 0 to this variable
    6.         lstDisplay.Items.Clear() 'clears the list box of any old data
    7.         For n = LBound(scores) To UBound(scores) 'loop to establish the limits on the array and assigns the value 20 to the variable n
    8.         Next 'ends the loop
    9.         For a As Double = LBound(scores) To UBound(scores) 'loop to total the exam scores and calculate the standard deviation
    10.             add += scores(a) 'totals the exam scores
    11.             sd = Math.Sqrt((scores(a) - mean) ^ 2 / n) 'formula for finding the standard deviation
    12.         Next
    13.         mean = (add / n) 'formula for finding the mean
    14.         outputInfo(add, mean, sd, n, grade) 'establishes the parameters for the sub procedure
    15.     End Sub
    16.     Sub outputInfo(ByRef add As Double, ByRef mean As Double, ByRef sd As Double, ByRef n As Double, ByVal grade As String)
    17.         Dim fmtStrHeader As String = "{0,-10}{1,12}" 'establishes my string header
    18.         lstDisplay.Items.Add("There were " & n & " exams.") 'displays the total amount of exams
    19.         lstDisplay.Items.Add("Mean: " & mean) 'displays the mean
    20.         lstDisplay.Items.Add("Std. Deviation: " & FormatNumber(sd, 2)) 'displays the standard deviation
    21.         lstDisplay.Items.Add("") 'skips a line
    22.         lstDisplay.Items.Add(String.Format(fmtStrHeader, "Score", "Grade")) 'displays the columns for score and grade
    23.         For m As Integer = LBound(scores) To UBound(scores) 'Loop for establishing which letter grade goes with which scores
    24.             If scores(m) >= mean + (1.5 * sd) Then 'determines what score gets an A
    25.                 grade = "A"
    26.             ElseIf mean + (0.5 * sd) <= scores(m) And scores(m) < mean + (1.5 * sd) Then 'determines what score gets a B
    27.                 grade = "B"
    28.             ElseIf mean - (0.5 * sd) <= scores(m) And scores(m) < mean + (0.5 * sd) Then 'determines what score gets a C
    29.                 grade = "C"
    30.             ElseIf mean - (1.5 * sd) <= scores(m) And scores(m) < mean - (0.5 * sd) Then 'determines what score gets a D
    31.                 grade = "D"
    32.             ElseIf (scores(m) < mean - (1.5 * sd)) Then 'determines what score gets a F
    33.                 grade = "F"
    34.             End If
    35.             lstDisplay.Items.Add(String.Format(fmtStrHeader, scores(m), grade)) 'displays the scores with their corresponding grade
    36.         Next 'closes the loop
    37.     End Sub 'tells the program where the sub procedure ends

    I hope that is readable. Anyways, its working and you guys definitely helped. Thanks a ton.

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    10

    Re: Help with arrays

    Also, as for the formula for finding the standard deviation, I'm using the formula that they have written in the book. This is a VB book and not a statistics book. Plus, this particular book is riddled with typos. So, in an effort to save myself any extra headache, I am going to use this formula since it is given with the assigned project. Not that your formula doesn't work or isn't good enough, Ellis, its just that this is what the teacher will be using to determine the accuracy of my program. I'm sure you understand. Again, I couldn't have done it without your guidance. Thanks.

  12. #12
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Help with arrays

    That's VB.NET; this is the classic VB (VB6 and VBA) forum. I'm guessing the things we posted that aren't in your book aren't actually part of VB.NET.

  13. #13

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    10

    Re: Help with arrays

    Haha. My apologies. I'm such a greenhorn.

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