Option Explicit On
'My Array Tools version 1.0
Public Class TArray
Public Function Average(ByVal Source As Object) As Integer
Dim iLen As Integer = (Source.Length - 1)
'Return average
Return Sum(Source) \ iLen
End Function
Public Function Sum(ByVal Source As Object) As Integer
Dim iLen As Integer = (Source.Length - 1)
Dim iSum As Integer
'Sum up values only works with numbers
For x As Integer = 0 To iLen
Try
iSum += Source(x)
Catch ex As Exception
iSum = 0
End Try
Next
Return iSum
End Function
Public Function Middle(ByVal Source As Object) As Object
Dim iLen As Integer = (Source.Length - 1)
Dim iMiddle As Integer = 0
'Find middle in array
For x As Integer = 0 To iLen
If Source(x) < iLen Then
iMiddle = x
End If
Next
Return iMiddle
End Function
Public Sub Shuffle(ByRef Source As Object)
Dim iLen As Integer = (Source.Length - 1)
For x As Integer = 0 To iLen
Dim Seed As Integer = Int(Rnd() * x)
'Do random swap
Swap(Source(x), Source(Seed))
Next
End Sub
Private Sub Swap(ByRef a, ByRef b)
Dim t = b
b = a
a = t
End Sub
Public Sub Reverse(ByRef Source As Object)
Dim iLen As Integer = (Source.Length - 1)
Dim Tmp() As Object
ReDim Preserve Tmp(0 To iLen)
For x As Integer = iLen To 0 Step -1
Tmp(iLen - x) = Source(x)
Next
'Return new array.
Source = Tmp
Erase Tmp
End Sub
Public Sub ArraySwapElement(ByVal Source As Object, ByVal iStart As Integer, ByVal iEnd As Integer)
Try
'Swap array values.
Swap(Source(iStart), Source(iEnd))
Catch ex As Exception
Throw New ArgumentException(ex.Message)
End Try
End Sub
Public Function Max(ByRef Source As Object) As Integer
Dim MaxInt As Integer = 0
Dim iLen As Integer = (Source.Length - 1)
For x As Integer = 0 To iLen
If Source(x) > Source(MaxInt) Then
MaxInt = x
End If
Next
Try
Return Source(MaxInt)
Catch ex As Exception
Return 0
End Try
End Function
Public Function Min(ByRef Source As Object) As Integer
Dim MinInt As Integer = 0
Dim iLen As Integer = (Source.Length - 1)
For x As Integer = 0 To iLen
If Source(x) < Source(MinInt) Then
MinInt = x
End If
Next
Try
Return Source(MinInt)
Catch ex As Exception
Return 0
End Try
End Function
Public Sub BubbleSort(ByRef Source)
Dim iLen As Integer = Source.length
For x As Integer = 0 To iLen
For j As Integer = x + 1 To iLen
If Source(x) > Source(j - 1) Then
Swap(Source(j - 1), Source(x))
End If
Next
Next
End Sub
End Class