Home > Software engineering >  Questions about the VBS random two-dimensional array
Questions about the VBS random two-dimensional array

Time:09-26

Ask god to help with the following produce random function in the VBS randomly generated two-dimensional array of N * N, find the maximum and minimum values, output its position,
Reference:

 '- generated within the specified interval [LowerBound, UpperBound] Num is a uniform distribution of integer 
Private Function GetIntegers (ByVal LowerBound, ByVal UpperBound, ByVal Num)
Dim I
Dim re ()
ReDim re (Num 1)

Randomize the Timer
For I=0 To Num 1
Re (I)=Int (Rnd () * (UpperBound LowerBound + 1) + LowerBound)
Next

GetIntegers=re
End the Function

CodePudding user response:

In the building's head so beautiful's sake:
 Private Sub Command1_Click () 
The Test 5
End Sub
'with the following generate random function in the VBS randomly generated two-dimensional array of N * N, find the maximum and minimum values, output its position,
Private Sub Test (N As Long)
Dim y, x
Dim (A)
Dim V
Dim maxV, maxY, maxX
Dim minV minY, minX

ReDim A (N - 1, N - 1)
For y=0 To N - 1
V=GetIntegers (0, 9, N)
For x=0 To N - 1
A (y, x)=V (x)
The Debug. Print ""; V (x);
Next
The Debug. Print
Next
MaxY=0: maxX=0: maxV=A (0, 0)
MinY=0: minX=0: minV=A (0, 0)
For y=0 To N - 1
For x=0 To N - 1
If maxV & lt; A (y, x) Then maxV=(y, x) : A maxY=y: maxX=x
If minV & gt; A Then minV (y, x)=A (y, x) : minY=y: minX=x
Next
Next
The Debug. Print "maxV, maxY, maxX="; MaxV; ", "; MaxY. ", "; MaxX
The Debug. Print "minV, minY, minX="; MinV; ", "; MinY; ", "; MinX
End Sub

'- generated within the specified interval [LowerBound, UpperBound] Num is a uniform distribution of integer
Private Function GetIntegers (ByVal LowerBound, ByVal UpperBound, ByVal Num) As the Variant
Dim I
Dim re ()

ReDim re (Num 1)
Randomize the Timer
For I=0 To Num 1
Re (I)=Int (Rnd () * (UpperBound LowerBound + 1) + LowerBound)
Next
GetIntegers=re
End the Function


  • Related