> Анютка
Только ради развлечения
Option Explicit
Sub main()
Dim ar As Variant
ar = Array(-5, 2, 5, -3, -7.08, 2.32, 6, 1, 0.00001, 2.007, -1, -8.5, 9.32, 8, 4.87, 8, 3, -5, 0, 0.25, 0.1)
TestOnMinValue ar
End Sub
Public Sub TestOnMinValue(ar As Variant)
Dim out As Variant
out = GetPositiveOnly(ar)
Dim mini As Variant
mini = Minimum(out)
Debug.Print "Minimal value is: " & mini
Dim mult As Variant
mult = ProductOfArray(ar)
Debug.Print "Product of an array is: " & mult
If CDec(mini) > CDec(mult) Then
Debug.Print "Product of an array is: " & mult & ". Third item in array is " & ar(2)
Else
Debug.Print "Minimal element is less than" & vbCr & "product of the given array"
End If
End Sub
Public Function Minimum(ar As Variant) As Variant
Dim i As Integer, mini As Variant
mini = Null
For i = 0 To UBound(ar)
If IsNull(mini) Then
mini = ar(i)
ElseIf Not IsNull(ar(i)) Then
If mini > ar(i) Then
mini = ar(i)
End If
End If
Next i
Minimum = mini
End Function
Public Function ProductOfArray(ar As Variant) As Variant
Dim res As Variant
Dim i As Long
res = 1
For i = LBound(ar) To UBound(ar)
res = res * ar(i)
Next
ProductOfArray = res
End Function
Public Function GetPositiveOnly(ar As Variant) As Variant
Dim res() As Variant
Dim i As Long, j As Long
For i = LBound(ar) To UBound(ar)
If ar(i) > 0 Then
ReDim Preserve res(j)
res(j) = ar(i)
j = j + 1
End If
Next
GetPositiveOnly = res
End Function
~'J'~