by Paul Bradley (2 Submissions)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 15th December 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)
This code creates an array and then finds the max and min values of the array
API Declarations
Dim Values As Integer
Dim stopit As Boolean
Dim counter As Integer
Dim items As Integer
'This program was created by Paul Bradley 9/12/00
'This program Requires the following commands and items on the form
'Cmd_Create Command button
'Cmd_Max Command button
'Cmd_Min Command button
'Cmd_Quit Command button
'List1 Listbox
'Text_Answer Textbox
Dim Testarray(1 To 100) As Integer ' Array of Random numbers between 0 and 1000
Dim Values As Integer
Dim stopit As Boolean
Dim counter As Integer
Dim items As Integer
Private Sub Cmd_Create_Click()
' First we need to create an array of numbers
List1.Clear ' Delete previous list
counter = 0 ' Reset the counter
stopit = False ' Reset stopit
If counter = 0 Then
Do ' Start Loop
counter = counter + 1 ' Start Counter
Randomize
items = (10000 * Rnd) + 1 ' Create values for array
Testarray(counter) = items ' Add random value to the array
If counter = 100 Then ' Stop creating array
stopit = True
End If
List1.AddItem counter & ":" & Testarray(counter) ' Create list from the array
DoEvents
Loop Until stopit ' Stop the loop when counter reaches 100
End If
End Sub
Private Sub Cmd_Max_Click()
' Find the maximum value of the array
If List1.ListCount = 0 Then
MsgBox ("You havent created the array yet")
Else
' Find the maximum value of the array
Maximum = Testarray(1) ' Declare the first value of the array as maximum
For Values = 1 To UBound(Testarray) ' See bellow for Ubound
If Testarray(Values) > Maximum Then Maximum = Testarray(Values) ' This section is the important part. See bellow
Next Values
Text_Answer.Text = Maximum ' Show the maximum value in the text box
End If
End Sub
Private Sub Cmd_Min_Click()
' Find the maximum value of the array
If List1.ListCount = 0 Then
MsgBox ("You havent created the array yet")
Else
' Find the minimum value of the array
Minimum = Testarray(1) ' Declare the first value of the array as minimum
For Values = 1 To UBound(Testarray) ' See bellow for Ubound
If Testarray(Values) < Minimum Then Minimum = Testarray(Values) ' This section is the important part. See bellow
Next Values
Text_Answer.Text = Minimum ' Show the minimum value in the text box
End If
End Sub
Private Sub Cmd_Quit_Click()
'End the program and exit to windows
End
End Sub
Private Sub Form_Load()
' Start Program
stopit = False
End Sub
' Notes *************************************************************************
' Ubound function finds the maximum number of the array in this case it is 100.
' The reason i used this is because the project I am working on has arrays that
' vary in length. For more information MS Visual Basic help
'
' The program is very simple. To find the maximum or minimum value set the first
' value of the array as maximum or minimum. The program goes through the array
' from 1 to 100 (Ubound). If the value is greater then the set maximum value then
' The value is set as that value. If the next value is lower than the set value
' Then the program carries on the next value in the array. The reverse is true for
' for the minimum. If the value is lower then that is the new value
'
' Mail comments to [email protected]; [email protected]
No comments have been posted about This code creates an array and then finds the max and min values of the array. Why not be the first to post a comment about This code creates an array and then finds the max and min values of the array.