'
'CS12-Chris Wallace
'array5_Standard Deviation
'
Public Class Form1
    '********************************************************
    '******************** PROJECT OUTLINE *******************
    '********************************************************
    '1. find total of aryA
    '2. find average of aryA
    '3. populate aryD with the deviation (difference between aryA and the average)
    '4. find the average of the deviations
    '5. find the sum of aryD^2
    '6. find the SQRT of aryD^2
    '********************************************************
    '********************************************************


    Const MAX As Integer = 50
    Dim aryIndex As Integer = 0

    Dim aryA(MAX) As Integer
    Dim aryD(MAX) As Double



    

    '===Handlers===
    Private Sub btnFillAryA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillAryA.Click
        '===fills aryA
        Const FILLAMOUNT As Integer = 15
        Call fillAry(aryA, FILLAMOUNT)
    End Sub
    Private Sub btnShowAryA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAryA.Click
        '===shows aryA in the list box
        Call clearOut()         'clears the list box
        For i As Integer = 1 To aryIndex
            Call showOut(aryA(i))
        Next
    End Sub
    Private Sub btnSumA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSumA.Click
        '===gets and displays the sum of the elements in aryA
        Dim sum As Integer = 0
        sum = calcSum(aryA, aryIndex)
        Call showOut("Total for Array A: " & sum)
    End Sub
    Private Sub btnAvgA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAvgA.Click
        '===gets and displays the average of the elements in aryA
        Dim avg As Double = 0
        avg = calcAvg(calcSum(aryA, aryIndex), aryIndex)
        Call showOut("The average value for Array A is: " & avg)
    End Sub
    Private Sub btnFillAryD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillAryD.Click
        '===fills aryD with the deviations from aryA and the average
        Call fillAry(aryD, aryA, aryIndex)
    End Sub



    
    '===Process Subs/Functions===
    Sub fillAry(ByVal a() As Integer, ByVal m As Integer)
        For i As Integer = 1 To m
            a(i) = i              'puts a value into the next element of the array
            aryIndex += 1         'increases the array Index by 1
        Next
    End Sub
    Sub fillAry(ByVal d() As Double, ByVal a() As Integer, ByVal m As Integer)
        '@@@ 
        Call clearOut()
        For i As Integer = 1 To m
            'puts a value into the next element of the array
            Dim avgval As Double
            avgval = calcAvg(calcSum(aryA, aryIndex), aryIndex)
            '@@@    Call showOut("avgval = ", avgval)
            'd(i) = calcDev(aryA, avgval)
            d(i) = calcDev(aryA, i, avgval)
            '@@@
            Call showOut(d(i))
        Next

    End Sub
    Function calcSum(ByVal a() As Integer, ByVal k As Integer) As Integer
        '===calculates the sum of the elements in an array
        Dim total As Integer = 0
        For i As Integer = 1 To k
            total += i
        Next
        Return total
    End Function
    Function calcAvg(ByVal t As Integer, ByVal c As Integer) As Double
        '===calculates the average
        Return (t / c)          'total divided by count
    End Function
    Function calcDev(ByVal a() As Integer, ByVal i As Integer, ByVal avg As Double) As Double
        Dim deviation As Double
        deviation = (a(i) - avg)
        Return deviation
    End Function
    '===Output Subs===
    Sub clearOut()
        Me.lstOut.Items.Clear()
    End Sub
    Sub showOut(ByVal s As String)
        Me.lstOut.Items.Add(s)
    End Sub
    Sub showOut(ByVal s As String, ByVal t As String)
        Me.lstOut.Items.Add(s & t)
    End Sub
    Sub showOut(ByVal s As String, ByVal i As Integer)
        Me.lstOut.Items.Add(s & Format(i))
    End Sub
    Sub showOut(ByVal s As String, ByVal d As Double)
        Me.lstOut.Items.Add(s & Format(d))
    End Sub











 
End Class
'
'CS12-Chris Wallace
'array5_Standard Deviation
'