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
Comment