CS375 One-Dimensional Array Example in Visual Basic 6.0 Option by olliegoblue23

VIEWS: 0 PAGES: 1

									CS375                               One-Dimensional Array Example in Visual Basic 6.0
Option Explicit
Option Base 1
'One-dimensional Array example
Private Sub cmdCompute_Click()
  'Declare an array of 20 elements
  Dim Array1(1 To 20) As Integer, index As Integer, EvenC As Integer, OddC As Integer, _
     EvenS As Integer, OddS As Integer, Avg As Double
  'Pass array to subprocedure
  Call Load_Array(Array1)
  Call TestEvenOdd(Array1, EvenS, OddS, EvenC, OddC)
  Call PrintData(Array1)
  Avg = (EvenS + OddS) / UBound(Array1)
  picOut.Print "Results of Testing Data Above/Below Average:"
  picOut.Print
  For index = LBound(Array1) To UBound(Array1)
     'Pass an array element to a function
     If Average(Array1(index), EvenS, OddS, Avg) Then
        picOut.Print Array1(index); "is above the average of "; Format$(Avg, "fixed")
     Else
        picOut.Print Array1(index); "is below the average of "; Format$(Avg, "fixed")
     End If
  Next index
End Sub
Private Sub Load_Array(Data() As Integer)
  Dim x As Integer
  Call Randomize
  For x = LBound(Data) To UBound(Data)
     Data(x) = 1 + Int(Rnd() * 100)
  Next x
End Sub
Private Sub TestEvenOdd(Values() As Integer, sumE As Integer, sumO As Integer, _
               countE As Integer, countO As Integer)
  Dim x As Integer
  For x = LBound(Values) To UBound(Values)
     If Values(x) Mod 2 = 0 Then
        sumE = sumE + Values(x)
        countE = countE + 1
     Else
        sumO = sumO + Values(x)
        countO = countO + 1
     End If
  Next x
  picOut.Print "Summary Results are:"
  picOut.Print
  picOut.Print "Sum of Even Numbers ="; sumE
  picOut.Print "Count of Even Numbers ="; countE
  picOut.Print "Sum of Odd Numbers ="; sumO
  picOut.Print "Count of Odd Numbers ="; countO
  picOut.Print
End Sub
Private Function Average(Results As Integer, sumE As Integer, sumO As Integer, mean As Double) _
  As Boolean
  If Results > mean Then
     Average = True
  ElseIf Results <> mean Or Results < mean Then
     Average = False
  End If
End Function
Private Sub PrintData(Results() As Integer)
  Dim x As Integer
  picOut2.Print "The Data Generated are :"
  picOut2.Print
  For x = LBound(Results) To UBound(Results)
     picOut2.Print "Index="; x; " Element="; Results(x)
  Next x
End Sub

								
To top