943,630 Members | Top Members by Rank

Ad:
Apr 25th, 2007
0

Help with simple Array program

Expand Post »
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!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marlon ng is offline Offline
10 posts
since Apr 2007
Apr 25th, 2007
1

Re: Help with simple Array program

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
frequency(5)=frequency(5)+1

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

-Mike
Reputation Points: 14
Solved Threads: 2
Newbie Poster
cricket2030 is offline Offline
19 posts
since Feb 2007
Apr 26th, 2007
1

Re: Help with simple Array program

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.
Reputation Points: 26
Solved Threads: 5
Junior Poster
scudzilla is offline Offline
191 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Getting A Data From Ms Eccess And Putting It In An Existing Excel File Using Vb 6.0
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Text File ammendments





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC