数组arr中共有40个元素,以下代码是用于计算数组arr中每个元素与其下标相除所得的和,即arr(1)/1 + arr(2)/2 + arr(3)/3 + ... + arr(40)/40
Option Base 1
Function Fun(a() As Single) As Single
Dim i As Integer
Dim sum As Single
sum = 0
For i = 1 To UBound(a)
sum = sum + a(i) / i
Fun = sum
Next
End Function
Private Sub Command1_Click()
Dim arr
Dim arr2(40) As Single
arr = Array(12.5, 65.4, 56, 73, 46.23, 58.25, 83.49, 96, 27.3, 36.78, 29.26, 89.4, 34.7, 45.4, 56.9, 67.4, 78, 98, 87.5, 76.8, 34.6, 55.8, 64.7, 43.7, 99.4, 84.7, 66.2, 23.9, 55.5, 44.1, 33.2, 43.5, 41.2, 28.3, 78.6, 98.3, 56.1, 73.6, 49.3, 48.3)
For i = 1 To UBound(arr)
arr2(i) = arr(i)
Next i
r = Fun(arr2())
Text1.Text = Str(r)
SaveData
End Sub
Sub SaveData()
Open "out3.txt" For Output As #1
Print #1, Text1.Text
Close 1
End Sub
谢谢!
阿晨1998
拉丁的传说
相关分类