Programming Tips and Tricks

Summing a List
Average of a List
Largest in a List
Smallest in a List
Random List of Nums
Shuffling a List
Insert an Item
Sorting a List
Removing Items
Counting Even Numbers
Counting Odd Numbers
Rolling Two Dice
Dealing a Card
Rounding a Number
Formatting Output
Horizontal Printing
Time Delay
Time Delay 2
Animate - Up/Down
Animate - Linear
Animate - Parabola

Tricks and Fun Stuff with Timers

Click to Run Fun Stuff Program

Click to View Fun Stuff Source Code

 

Summing a List

   SUM = 0
   Begin Loop
       get next NUM
       SUM = SUM + NUM
   Until end of list

 

<Top>

Average of a List

   SUM=0
   CNT=0
   Begin Loop
       get next NUM
       SUM = SUM + 1
       CNT = CNT + 1
   Until end of list
   AVG = SUM / CNT

Largest in a List

   BIG = first NUM
   Begin Loop
      get next NUM
      If  NUM > BIG
         BIG = NUM
   Until end of list

<Top>

Smallest in a List

   SMALL = first NUM
   Begin Loop
      get next NUM
      If  NUM < SMALL
         SMALL = NUM
   Until end of list

Counting Even Numbers

Uses the "mod" operator

   CNT = 0
   Begin Loop
      get next NUM
      If  NUM mod 2 = 0
         CNT = CNT + 1
   Until end of list

<Top>

Counting Odd Numbers

Uses the "mod" operator

   CNT = 0
   Begin Loop
      get next NUM
      If  NUM mod 2 = 1
         CNT = CNT + 1
   Until end of list

 

Rolling two Dice

   R1 = random 1 - 6
   R2 = random 1 - 6
   SUM = R1 + R2

 

<Top>

Dealing a Card

   V = "23456789TJQKA"
   S = "HDSC"
   RV = random 1 - 13
   RS = random 1 - 4
   CARD = char in pos RV of V  &
                    char in pos RS of S

Rounding a Number to 2 decimals

Suppose NUM = 12.9572

   NUM = Int (100 * NUM + 0.5) / 100

NUM now equals 12.96

<Top>

Formatting Output

Suppose PRICE = 12.9572

   lblAnswer.Caption = Format(PRICE, "$0.00")

will output   $12.96

 

Horizontal Printing

To print the following:

1  2  3  4  5  6  7  8  9   10
All Done

   For X = 1 to 10
      print X ;
   Next X
   print
   print "All Done"

<Top>

Time Delay - for delays > 0.01 sec.

To cause a delay of S seconds
Use: 
Call Delay(5)
for a 5 second delay

  Private Sub Delay(S as Single)
     Dim T as Single
     T = Timer
     Do
     Loop Until Timer - T > S
  End Sub

Generating a Random List

This code fills an array with 10 random
numbers, no repetitions

   CNT=1
   LIST(CNT) = random number
   Do
      NUM = random number
      FLAG = 0
      For I = 1 to CNT
         If LIST(CNT) = NUM then FLAG = 1
      Next I
      If FLAG = 0 then
         CNT = CNT + 1
         LIST(CNT) = NUM
      End If
   Loop Until CNT = 10

<Top>

Sorting a List of Numbers

This code SORTS a LIST of 10
numbers from Highest to Lowest

   For I = 1 to 9
      BIG = LIST(I)
      For J = I+1 to 10
         If LIST(J) > BIG Then
            TEMP = LIST(J)
            LIST(J) = LIST(I)
            LIST(I) = TEMP
         End If
      Next J
   Next I

Shuffling a List of Numbers

This code shuffles a list by switching
pairs of numbers 1000 times

   For I = 1 to 1000
      A = random index
      B = random index
      TEMP = LIST(A)
      LIST(A) = LIST(B)
      LIST(B) = TEMP
   Next I

<Top>

Removing Items from a List

This code removes Bad items and
moves the Good ones up to fill in gaps

   P = 0
   For I = 1 to SIZE
      If LIST(I) is Good Then
         P = P+1
         LIST(P) = LIST(I)
      End if
   Next I
   SIZE = P

Insert an Item in a List

This code inserts a new item at
position P in a list by moving items
down to make room.

   If P <= SIZE Then
      for I = SIZE to P step -1
         LIST(I+1) = LIST(I)
      Next I
   End If
   LIST(P) = new item
   SIZE = SIZE + 1

<Top>

Time Delay 2 - for delays < 0.01 sec.

Use in animations where a delay
involving the system clock gives
bad results. You have to experiment
with the value of S to use.

  Private Sub Delay(S as Long)
     Dim N as Long
     Do
          N = N + 1
     Loop Until N > 1000 * S
  End Sub

Upward motion:   starts at Y=B
and goes up to Y=A

   For Y = B to A step -1
       shpBall.Top = Y
       call Delay ...
   Next Y

<Top>

Downward motion:  starts at Y=A
and falls down to Y=B

   For Y = A to B
       shpBall.Top = Y
       call Delay ...
   Next Y

Motion in a straight line:  moves a
ball from (X1,Y1) to (X2,Y2) in a
straight line path

   DX = X2 - X1
   DY = Y2 - Y1
   H = Int(Sqr(DX^2 + DY^2))
   DX = DX / H
   DY = DY / H
   For I = 1 to H
       X = X+DX
       Y = Y+DY
       shpBall.Left = X
       shpBall.Top = Y
       call Delay ...
   Next I

<Top>

Motion in a Parabolic Path
Moves ball from (A,D) and (B,D)
in a parabolic path. You must
experiment with H to get the
proper height.

   For X = A to B
       Y = H * (X-A) * (X-B) + D
       shpBall.Left = X
       shpBall.Top = Y
       call Delay ...
   Next X

   
   
   

Fun Stuff with Timers

Option Explicit
Dim X As Integer
Dim SEC As Integer
Dim DIS As Integer

'Trick 1 - Switch Graphics
'Uses 2 Image Boxes, one visible and one not
'Reverses the visible property of each box

Private Sub cmdTrick1_Click()
   imgHap.Visible = Not imgHap.Visible
   imgSad.Visible = Not imgSad.Visible
End Sub

'Trick 2 - Blinker
'Uses a Timer Object to Reverse the Visible
'property of an image
'START button enables the Timer

Private Sub cmdT2Start_Click()
   tmrBull.Enabled = True
End Sub
'STOP button disables the Timer
Private Sub cmdT2Stop_Click()
   tmrBull.Enabled = False
   imgBull.Visible = True
End Sub
'Timer reverses the Visible state
Private Sub tmrBull_Timer()
   imgBull.Visible = Not imgBull.Visible
End Sub

'Trick 3 - Slider
'Uses a timer object to slide a picture

Private Sub cmdT3Start_Click()
   X = 0
   tmrCam.Enabled = True
End Sub
'Timer moves the Camera 50 units and returns it
'to the start after going 1000 units

Private Sub tmrCam_Timer()
   X = X + 50
   picCam.Left = picCam.Left + 50
   If X = 1000 Then
      X = 0
      picCam.Left = picCam.Left - 1000
   End If
End Sub
'Stop button disables the timer and returns
'the picture to the start

Private Sub cmdT3Stop_Click()
   tmrCam.Enabled = False
   picCam.Left = picCam.Left - X
End Sub

'Trick 4 - Flasher
'Start button enables a timer which switches
'the FillStyles of two shapes

Private Sub cmdT4Start_Click()
   tmrFlash.Enabled = True
End Sub
'Stop button disables the timer
Private Sub cmdT4Stop_Click()
   tmrFlash.Enabled = False
End Sub
'Timer changes the fillstyles of the red
'and green circles

Private Sub tmrFlash_Timer()
   If shpRed.FillStyle = 0 Then
      shpRed.FillStyle = 1
      shpGrn.FillStyle = 0
   Else
      shpRed.FillStyle = 0
      shpGrn.FillStyle = 1
   End If
   Beep
End Sub

'Trick 5 - Countdown
'Enables a timer which counts down 10 seconds

Private Sub cmdT5Start_Click()
   cmdT5Start.Enabled = False
   SEC = 10
   lblClock = SEC
   tmrClock.Enabled = True
End Sub
'Timer decreases count and resets at 0
Private Sub tmrClock_Timer()
   SEC = SEC - 1
   lblClock = SEC
   If SEC = 0 Then
      tmrClock.Enabled = False
      Beep
      cmdT5Start.Enabled = True
   End If
End Sub

'Trick 6 - Rocket
'Enables a timer to move the rocket

Private Sub cmdT6Start_Click()
   tmrRocket.Enabled = True
End Sub
'Timer moves rocket upward in the image box
'until it reaches the top. It then resets the
'position of the rocket.

Private Sub tmrRocket_Timer()
   DIS = DIS + 50
   picRocket.Top = picRocket.Top - 50
   If DIS = 2100 Then
      DIS = 0
      picRocket.Top = picRocket.Top + 2100
      tmrRocket.Enabled = False
   End If
End Sub

Private Sub cmdExit_Click()
   Unload Me
   End
End Sub

<Top>
This is the source code for the Timer Tricks

Click [Here] to run the program

Trick 1 uses 2 image boxes placed on top of each other. Make sure you set the visible property of the sad face to FALSE and the happy face to TRUE during design.

 

 

Trick 2 uses a Timer object with interval 200. It must be disabled during design. When the Start button is clicked the Timer is enabled and the bullseye will flash off and on until you click the STOP button.

 

 

 

 

 

 

Trick 3 is fairly complex. A Timer is used to slide a picture box 1000 units. The timer has an interval of 100 and is disabled during design. It is activated when the Start button is clicked. Once the picture has moved 1000 units it is sent back to the start position and moves again.

 

 

 

 

 

 

 

 

See if you can figure out how the remaining tricks work.