VB.NET - Cheat sheet for calculating average



< strong>Code to calculate average

'function to calculate avarage 
 Function CalcAverage(ByVal ParamArray val() As Integer) As Double
        Dim sum As Double = 0
        For i As Integer = LBound(val) To UBound(val)
            sum += val(i)
        Next
        Dim length As Double = val.Length
        Dim average As Double = sum / length
        Return average
        Return sum
    End Function

Calling the method

    Private Sub btnSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'output :3
        MessageBox.Show(CalcAverage(1, 2, 3, 4, 5))
        'output :6.333333
        MessageBox.Show(CalcAverage(4, 5, 10))
    End Sub

Share |

 Cant find the page you are looking for?
 Help us to improve by adding the content that you are looking for.
 Leave a feedback
 We look forward to hear your comments and feedback.