Jump to content

how to find minimum in array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Lahsa

Lahsa

    Newbie

  • Members
  • Pip
  • 2 posts
i can find min and max in this case, i have a problem with timer2

Private Sub Form_Load()

Timer2.Interval = 1

End Sub


Private Sub Timer2_Timer()

Dim a(10) As Integer

Timer2.Interval = Timer2.Interval + 1

If Timer2.Interval = 20 Then

   i = 1

   Do While i < 10

          

        If i = 10 Then

         Exit Do

      Else

         i = i + 1

      End If

                  

   a(i) = Abs(i - 15) / 2

   

Min = a(1)

For i = 1 To 10

If Min > a(i) Then

Min = a(i)

End If

Next i

 

Print Min


   

   Loop


   End If


End Sub


i can find min and max in this case

Private Sub Command1_Click()

A(1) = 5

A(2) = 34

A(3) = 2

A(4) = 1

A(5) = 100

A(6) = 8

i_min = LBound(A)

i_max = UBound(A)


max = 0

For i = i_min To i_max

If A(i) > max Then max = A(i)


Print A(i)

Next


Print min

Text1.Text = max

Text2.Text = min

End Sub

it is shown in text1=100 and text2=1, it works fine, but when i am trying with timer it does not work

#2
mH_p0rtal

mH_p0rtal

    Newbie

  • Members
  • PipPip
  • 10 posts
hi
it shows the max number of 10 bound array
Private Sub Timer2_Timer()
Dim a(10) As Integer
a(1) = 5
a(2) = 304
a(3) = 2
a(4) = 1
a(5) = 150
a(6) = 8
i_min = LBound(a)
i_max = UBound(a)

Max = 0
For i = i_min To i_max
If a(i) > Max Then Max = a(i)
Next

Print Max

End Sub


#3
Lahsa

Lahsa

    Newbie

  • Members
  • Pip
  • 2 posts
i want to find min in this code with timer and do while, but it does not work

Private Sub Form_Load()

Timer2.Interval = 1

End Sub


Private Sub Timer2_Timer()

Dim a(10) As Integer

Timer2.Interval = Timer2.Interval + 1

If Timer2.Interval = 20 Then

   i = 1

   Do While i < 10

          

        If i = 10 Then

         Exit Do

      Else

         i = i + 1

      End If

                  

   a(i) = Abs(i - 15) / 2

   

Min = a(1)

For i = 1 To 10

If Min > a(i) Then

Min = a(i)

End If

Next i

 

Print Min


   Loop


   End If


End Sub



#4
mH_p0rtal

mH_p0rtal

    Newbie

  • Members
  • PipPip
  • 10 posts
hi
your source code was mess up , i made it more clear
now it prints the min number between 10 numbers

Private Sub Timer2_Timer()

Timer2.Interval = Timer2.Interval + 1


If Timer2.Interval = 20 Then

  Dim a(10) As Integer


   i = 1

   Do While i <= 10

          

 

                  

   a(i) = Abs(i - 15) / 2

   i = i + 1

   

   


Loop

For i = 1 To 10

MsgBox a(i)


Next i


Min = a(1)

For i = 1 To 10

If Min > a(i) Then

Min = a(i)

End If

Next i

 

Print Min


   End If


End Sub


Private Sub Form_Load()

Timer2.Interval = 1


End Sub



this part is just for testing , remove it :
For i = 1 To 10

MsgBox a(i)


Next i