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