| | |
Help with simple Array program
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2007
Posts: 10
Reputation:
Solved Threads: 0
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!!!
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
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
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
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!
![]() |
Similar Threads
- array program, dont know where to even begin! (C)
- help needed with my assignment (C)
- Simple 2d array, please help.! (VB.NET)
- Simple 2d array help (Visual Basic 4 / 5 / 6)
- Is This an Array Program??? (C++)
- writing a simple cat program (C)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Getting A Data From Ms Eccess And Putting It In An Existing Excel File Using Vb 6.0
- Next Thread: Text File ammendments
Views: 3001 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Visual Basic 4 / 5 / 6
* 6 429 2007 access activex add age append application basic birth bmp c++ calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver struct subroutine table tags textbox time timer urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





