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!!!

mResponses(x) will always be a number from1 to 10. 'x' is the student number, mResponses(x) is that students response. So if that student says 5 then frequency(mResponses(x)) = frequency(5).

in the end you get

I know thats pretty much the same as the book's explanation, but sometimes when worded differant it can help.


commented: Got it! thanks a lot! +1

There are 10 different results. The frequency array stores the frequency of each result. For example, if the value of frequency(1) is 5, it means that out of 40 students, 5 gave a score of 1. In the code, for every student that gave a score of 1, frequency(1) will increase by one and the same for the others.

commented: Thanks a lot! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.