Help with simple Array program

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 10
Reputation: marlon ng is an unknown quantity at this point 
Solved Threads: 0
marlon ng marlon ng is offline Offline
Newbie Poster

Help with simple Array program

 
0
  #1
Apr 25th, 2007
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!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 16
Reputation: cricket2030 is an unknown quantity at this point 
Solved Threads: 2
cricket2030's Avatar
cricket2030 cricket2030 is offline Offline
Newbie Poster

Re: Help with simple Array program

 
1
  #2
Apr 25th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 181
Reputation: scudzilla is an unknown quantity at this point 
Solved Threads: 3
scudzilla's Avatar
scudzilla scudzilla is offline Offline
Junior Poster

Re: Help with simple Array program

 
1
  #3
Apr 26th, 2007
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.
There are 10 types of people: those who understand Binary and those who don't!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3001 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC