I got this from a book.:
Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means awful and 10 means excellent). Place the 40 responses in an Integer array and summarize the results of the poll.
The form has a command button named cmdPrint. Here is the code:
Option Explicit
Option Base 1
Dim mResponses(40) As Integer
Private Sub Form_Load()
Dim x As Integer
For x = LBound(mResponses) To UBound(mResponses)
mResponses(x) = 1 + Int(Rnd * 10)
Next x
End Sub
Private Sub cmdPrint_Click()
Dim frequency(10) As Integer '10 elements
Dim x As Integer
Call Cls
Call Randomize
'Calculate results
For x = LBound(mResponses) To UBound(mResponses)
frequency(mResponses(x)) = frequency(mResponses(x)) + 1
Next x
Print "Rating" & Space(3) & "Frequency"
For x = LBound(frequency) To UBound(frequency)
Print Space(3) & x & vbTab & vbTab & frequency(x)
Next x
cmdPrint.Enabled = False
End Sub
I don't understand the logic of this line:
frequency(mResponses(x)) = frequency(mResponses(x)) + 1
I've read the book's explanation over and over but I still don't get it.
Here is what the book says: "This statement increments the appropriate frequency counter depending on the value of mResponses(x). For example, when the counter x is 1, mResponses(x) is 1, so frequency(mResponses(x) is actually interpreted as frequency(1) = frequency(1) + 1 which increments array index one...." I don't understand it! How does this statement calculate the frequency of a response??
Many thanks guys!!!