1、
Private Sub Form_Click()
Dim A(1 To 10) As Integer
Dim i As Integer
Dim iMax As Integer, iPos As Integer
Me.Show
Me.Cls
Randomize
iMax = 0
For i = 1 To 10
A(i) = Fix(Rnd * 41) + 60
Print A(i) & " ";
If A(i) > iMax Then
iMax = A(i)
iPos = i
End If
Next i
Print iMax
Print iPos
End Sub
如果有好幾個數壹樣的話只能找到前面的(也算是壹個缺陷吧)
2.
Private Sub Command1_Click()
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Private Sub Command1_Click()
Dim a(1 To 10) As Integer
Dim i As Integer
Me.Show
Me.Cls
Randomize
For i = 1 To 10'顯示最初順序
a(i) = Fix(Rnd * 101) + 100
Print a(i) & " ";
Next i
Sort VarPtr(a(1))
For i = 1 To 10'輸出結果
Print a(i) & " ";
Next i
End Sub
Private Sub Sort(ByVal Head As Long)
Dim i As Integer, j As Integer, t As Integer
Dim b(1 To 10) As Integer
CopyMemory VarPtr(b(1)), Head, 20
For i = 1 To 9 '這節是排序的
For j = i + 1 To 10
If b(i) > b(j) Then
t = b(i)
b(i) = b(j)
b(j) = t
End If
Next j
Next i
CopyMemory Head, VarPtr(b(1)), 20
End Sub