could anyone help me understand this code whow it functions especially line 23

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Malouda
    New Member
    • May 2011
    • 2

    could anyone help me understand this code whow it functions especially line 23

    Code:
    1 ' Fig. 7.5: StudentPoll.vb
    2 ' Using arrays to display poll results.
    3
    4 Imports System.Windows.Forms
    5
    6 Module modStudentPoll
    7
    8 Sub Main()
    9 Dim answer, rating As Integer
    10 Dim output As String
    11
    12 ' student response array (typically input at run time)
    13 Dim responses As Integer()
    14 responses = New Integer() {1, 2, 6, 4, 8, 5, 9, 7, _
    15 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, _
    16 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10}
    17
    18 ' response frequency array (indices 0 through 10)
    19 Dim frequency As Integer() = New Integer(10) {}
    20
    21 ' count frequencies
    22 For answer = 0 To responses.GetUpperBound(0)
    23 frequency(responses(answer)) += 1
    24 Next
    25
    26 output &= "Rating " & vbTab & "Frequency " & vbCrLf
    27
    28 For rating = 1 To frequency.GetUpperBound(0)
    29 output &= rating & vbTab & frequency(rating) & vbCrLf
    30 Next
    31
    32 MessageBox.Show(output, "Student Poll Program", _
    33 MessageBoxButtons.OK, MessageBoxIcon.Information)
    34 End Sub ' Main
    35
    36 End Module ' modStudentPoll
    Fig. 7.5
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What line 23 does is take the answer given in response and increments the freqency array at that index. So if the response is 2, then it adds one to frequency(2). Which means that the number at frequency(2) is the number of people that responded with an answer of 2.

    Comment

    • Malouda
      New Member
      • May 2011
      • 2

      #3
      Thanks Rabbit but i still didn't get you well,line 23 says
      frequency(respo nse(answer))+=1 now when answer is 0 response(0)=1 then it becomes frequency(1)+=1 now is the 1 in the frequency(1)ref erencing an index?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        It doesn't become frequency(1). frequency(0) would = 1 in your example. frequency(1) would still = 0 until someone answers 1.

        Code:
        response = (1,2,1)
        response(0) = 1
        response(1) = 2
        response(2) = 1
        
        frequency(0) = 0
        frequency(1) = 0
        frequency(2) = 0
        
        frequency(response(0)) += 1
        frequency(1) += 1
        frequency(1) = 1
        
        frequency(response(1)) += 1
        frequency(2) += 1
        frequency(2) = 1
        
        frequency(response(2)) += 1
        frequency(1) += 1
        frequency(1) = 2

        Comment

        • Crusader2010
          New Member
          • Nov 2011
          • 13

          #5
          That code computes how many times a certain response was given. A response can be between 1 and 10. So,

          frequency(1) = frequency of response 1 = how many times 1 appears in responses.
          frequency(7) = frequency of response 7 = how many times 7 appears in responses.

          Now, since you have an array of responses, from 0 to responses.getup perbound(0), you cycle through this array and you increment the value of the corresponding frequency of the response when found. In other words, if response(i) = 8, you have frequency(8) +=1. Which is what line 23 is doing, frequency(respo nse(i))+=1. In your code 'i' = 'answer' as the iterator in the loop.

          Also, x += y means x = x+y

          Comment

          Working...