CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2006
    Posts
    8

    [RESOLVED] Help with simple Array program

    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:
    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!!!
    Last edited by sbstn26; April 26th, 2007 at 09:52 AM. Reason: to make it more readable

  2. #2
    Join Date
    Oct 2006
    Posts
    327

    Re: Help with simple Array program

    Hello,

    I suggest you use the "edit" button and use
    Code:
      CODE before your code and  /CODE at its End (both in between brackets "[]"
    , please, so, that it would be readable.. (do it please)

    frequency(mResponses(x)) = frequency(mResponses(x)) + 1
    as you randomize 10, you can get 10 !
    but Dim frequency(10) means 10 élementy i.e 0;1;2,3,4,5,6,7,8,9
    No 10 !
    Last edited by moa; April 25th, 2007 at 10:42 AM.

  3. #3
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: Help with simple Array program

    Quote Originally Posted by moa
    but Dim frequency(10) means 10 élementy i.e 0;1;2,3,4,5,6,7,8,9
    No 10 !
    Option Base 1 means that that Dim statement makes an array that goes from 1 to 10.
    Even Option Base 0 would have an index of 10 in that array, but you'd have 11 items (from 0 to 10).

    My suggestion, even if you use Option Base, explicitly define the size of arrays, like so:
    Code:
    Dim frequency(1 To 10) As Integer

    @sbstn26:
    The line of code:
    Code:
    frequency(mResponses(x)) = frequency(mResponses(x)) + 1
    It increments the counter for that value.

    The array frequency has an index of 1 to 10. That index corresponds to the ratings that can be given. That description you have means that the frequency array is used to store the number of times each rating is given.

    That example is poor, though.
    Code:
    Option Explicit
    Option Base 1
    Dim mResponses(40) As Integer
    
    Private Sub Form_Load()
        Dim x As Integer
        
        Call Randomize '<-- Where this should be called, since it makes the results random now
        
        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 '<-- No reason to do this here, Rnd isn't called in this Sub at all
        
        '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
    End Sub
    The above is corrected, but if you don't want to restart each time, use this instead:
    Code:
    Option Explicit
    Option Base 1
    Dim mResponses(40) As Integer
    
    Private Sub cmdPrint_Click()
        Dim frequency(10) As Integer '10 elements
        Dim x As Integer
        
        Call Randomize
        Call Cls
        
        For x = LBound(mResponses) To UBound(mResponses)
            mResponses(x) = 1 + Int(Rnd * 10)
        Next x
        
        'Calculate results
        For x = LBound(mResponses) To UBound(mResponses)
            frequency(mResponses(x)) = frequency(mResponses(x)) + 1
        Next x
        Print "Rating   Frequency"
        For x = LBound(frequency) To UBound(frequency)
            Print Space$(3) & x & vbTab & vbTab & frequency(x)
        Next x
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured