|
-
Nov 25th, 2009, 02:33 AM
#1
Thread Starter
New Member
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
-
Nov 25th, 2009, 02:48 AM
#2
Re: Help with arrays
 Originally Posted by Erexshin
..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)
-
Nov 25th, 2009, 03:05 AM
#3
Thread Starter
New Member
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?
-
Nov 25th, 2009, 03:08 AM
#4
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.
-
Nov 25th, 2009, 03:14 AM
#5
Thread Starter
New Member
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.
-
Nov 25th, 2009, 04:24 AM
#6
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:
Public Function Mean(pvarArray As Variant) As Double
Dim lngCount As Long
Dim dblTotal As Double
Dim i As Long
If Not IsArray(pvarArray) Then Exit Function
For i = LBound(pvarArray) To UBound(pvarArray)
lngCount = lngCount + 1
dblTotal = dblTotal + pvarArray(i)
Next
Mean = dblTotal / CDbl(lngCount)
End Function
Public Function StdDev(pvarArray As Variant, pdblMean As Double) As Double
Dim lngCount As Long
Dim dblTotal As Double
Dim i As Long
If Not IsArray(pvarArray) Then Exit Function
For i = LBound(pvarArray) To UBound(pvarArray)
lngCount = lngCount + 1
dblTotal = dblTotal + (pvarArray(i) - pdblMean) ^ 2
Next
If lngCount > 1 Then dblTotal = dblTotal / CDbl(lngCount - 1)
StdDev = Sqr(dblTotal)
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
-
Nov 25th, 2009, 01:03 PM
#7
Thread Starter
New Member
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.
-
Nov 25th, 2009, 07:52 PM
#8
-
Nov 25th, 2009, 11:24 PM
#9
Re: Help with arrays
 Originally Posted by Erexshin
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.
-
Nov 28th, 2009, 06:38 PM
#10
Thread Starter
New Member
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:
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 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim add, n, mean, sd As Double 'This declares which variables are to be used as a double data type Dim grade As String 'This declares which variables are to be used as a string data type add = 0 'assigns a base value of 0 to this variable lstDisplay.Items.Clear() 'clears the list box of any old data For n = LBound(scores) To UBound(scores) 'loop to establish the limits on the array and assigns the value 20 to the variable n Next 'ends the loop For a As Double = LBound(scores) To UBound(scores) 'loop to total the exam scores and calculate the standard deviation add += scores(a) 'totals the exam scores sd = Math.Sqrt((scores(a) - mean) ^ 2 / n) 'formula for finding the standard deviation Next mean = (add / n) 'formula for finding the mean outputInfo(add, mean, sd, n, grade) 'establishes the parameters for the sub procedure End Sub Sub outputInfo(ByRef add As Double, ByRef mean As Double, ByRef sd As Double, ByRef n As Double, ByVal grade As String) Dim fmtStrHeader As String = "{0,-10}{1,12}" 'establishes my string header lstDisplay.Items.Add("There were " & n & " exams.") 'displays the total amount of exams lstDisplay.Items.Add("Mean: " & mean) 'displays the mean lstDisplay.Items.Add("Std. Deviation: " & FormatNumber(sd, 2)) 'displays the standard deviation lstDisplay.Items.Add("") 'skips a line lstDisplay.Items.Add(String.Format(fmtStrHeader, "Score", "Grade")) 'displays the columns for score and grade For m As Integer = LBound(scores) To UBound(scores) 'Loop for establishing which letter grade goes with which scores If scores(m) >= mean + (1.5 * sd) Then 'determines what score gets an A grade = "A" ElseIf mean + (0.5 * sd) <= scores(m) And scores(m) < mean + (1.5 * sd) Then 'determines what score gets a B grade = "B" ElseIf mean - (0.5 * sd) <= scores(m) And scores(m) < mean + (0.5 * sd) Then 'determines what score gets a C grade = "C" ElseIf mean - (1.5 * sd) <= scores(m) And scores(m) < mean - (0.5 * sd) Then 'determines what score gets a D grade = "D" ElseIf (scores(m) < mean - (1.5 * sd)) Then 'determines what score gets a F grade = "F" End If lstDisplay.Items.Add(String.Format(fmtStrHeader, scores(m), grade)) 'displays the scores with their corresponding grade Next 'closes the loop 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.
-
Nov 28th, 2009, 07:02 PM
#11
Thread Starter
New Member
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.
-
Nov 29th, 2009, 01:27 AM
#12
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.
-
Nov 29th, 2009, 01:34 AM
#13
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|