天下足球感动:关于数组(VB6)

来源:百度文库 编辑:神马品牌网 时间:2024/05/02 07:30:58
如何对数组中的元素进行排序(大、小、平均值),最好给出例程。谢谢了

有单独求最大、最小、平均值的
也有通过排序找最大、最小的
希望对你有帮助~

Option Base 1

Private Sub Form_Click()

Dim aa(5) As Single
aa(1) = 1
aa(2) = 2
aa(3) = 3
aa(4) = 4
aa(5) = 5

Dim i As Single
Dim j As Single
Dim n As Single
Dim Max As Single
Dim Min As Single
Dim Ave As Single

'//只求最大值
If UBound(aa) <> 0 Then
Max = aa(1)
For i = 1 To UBound(aa)
If Max < aa(i) Then Max = aa(i)
Next i

'//只求最小值
Min = aa(1)
For i = 1 To UBound(aa)
If Min > aa(i) Then Min = aa(i)
Next i

'//只求平均值
For i = 1 To UBound(aa)
Ave = Ave + aa(i)
Next i

Ave = Ave / UBound(aa)

Print "Max=" & Max
Print "Min=" & Min
Print "Ave=" & Ave

End If

'//冒泡发排序(由小到大)
If UBound(aa) > 1 Then
For i = 1 To UBound(aa) - 1
For j = i + 1 To UBound(aa)
If aa(i) > aa(j) Then
n = aa(i)
aa(i) = aa(j)
aa(j) = n
End If
Next j
Next i

Print "最大值为:" & aa(1)
Print "最小值为:" & aa(UBound(aa))

End If

End Sub

是要举出数组中的数的最大值和最小值及它们的平均数吗?
Q我:158636740