473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. 1 ' Fig. 7.5: StudentPoll.vb
  2. 2 ' Using arrays to display poll results.
  3. 3
  4. 4 Imports System.Windows.Forms
  5. 5
  6. 6 Module modStudentPoll
  7. 7
  8. 8 Sub Main()
  9. 9 Dim answer, rating As Integer
  10. 10 Dim output As String
  11. 11
  12. 12 ' student response array (typically input at run time)
  13. 13 Dim responses As Integer()
  14. 14 responses = New Integer() {1, 2, 6, 4, 8, 5, 9, 7, _
  15. 15 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, _
  16. 16 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10}
  17. 17
  18. 18 ' response frequency array (indices 0 through 10)
  19. 19 Dim frequency As Integer() = New Integer(10) {}
  20. 20
  21. 21 ' count frequencies
  22. 22 For answer = 0 To responses.GetUpperBound(0)
  23. 23 frequency(responses(answer)) += 1
  24. 24 Next
  25. 25
  26. 26 output &= "Rating " & vbTab & "Frequency " & vbCrLf
  27. 27
  28. 28 For rating = 1 To frequency.GetUpperBound(0)
  29. 29 output &= rating & vbTab & frequency(rating) & vbCrLf
  30. 30 Next
  31. 31
  32. 32 MessageBox.Show(output, "Student Poll Program", _
  33. 33 MessageBoxButtons.OK, MessageBoxIcon.Information)
  34. 34 End Sub ' Main
  35. 35
  36. 36 End Module ' modStudentPoll
  37. Fig. 7.5
  38.  
  39.  
Nov 18 '11 #1
4 1481
Rabbit
12,516 Expert Mod 8TB
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.
Nov 18 '11 #2
Thanks Rabbit but i still didn't get you well,line 23 says
frequency(response(answer))+=1 now when answer is 0 response(0)=1 then it becomes frequency(1)+=1 now is the 1 in the frequency(1)referencing an index?
Nov 18 '11 #3
Rabbit
12,516 Expert Mod 8TB
It doesn't become frequency(1). frequency(0) would = 1 in your example. frequency(1) would still = 0 until someone answers 1.

Expand|Select|Wrap|Line Numbers
  1. response = (1,2,1)
  2. response(0) = 1
  3. response(1) = 2
  4. response(2) = 1
  5.  
  6. frequency(0) = 0
  7. frequency(1) = 0
  8. frequency(2) = 0
  9.  
  10. frequency(response(0)) += 1
  11. frequency(1) += 1
  12. frequency(1) = 1
  13.  
  14. frequency(response(1)) += 1
  15. frequency(2) += 1
  16. frequency(2) = 1
  17.  
  18. frequency(response(2)) += 1
  19. frequency(1) += 1
  20. frequency(1) = 2
Nov 18 '11 #4
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.getupperbound(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(response(i))+=1. In your code 'i' = 'answer' as the iterator in the loop.

Also, x += y means x = x+y
Nov 19 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Chris | last post by:
Hello Could anyone explain why the following: #footer ul { float : left; margin : 2px 0px 7px 28px; padding : 0px; width : 360px;
10
by: Chris | last post by:
Could anyone write a small program to log the Signal-to-Noise figures for a Netgear DG834 router? I have been getting very variable SNR readings - and I would like to collect some evidence to...
13
by: C++fan | last post by:
The following code is for list operation. But I can not understand. Could anyone explain the code for me? /* * List definitions. */ #define LIST_HEAD(name, type) struct name { type...
1
by: Mike | last post by:
I got the code below from an earlier post but I can't get it to work (I get an error on the "for (i=0; i<a.length; i++)" line) Anyone have code that works for cookies with keys? > Anyone...
2
by: Wiktor Zychla [C# MVP] | last post by:
Could anyone confirm/deny that following is a bug (or at least an "unexpected behaviour")? If this is not a bug, I would be glad for a short explanation or a workaround. Issue: A generic...
1
by: Andrew | last post by:
Hello, friends, I am implementing web app security using asp.net 1.1, and I found the following source code from Yahoo! Mail login page: <form method="post"...
2
by: Sean Hammond | last post by:
Anyone understand this? Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02) on linux2 Type "help", "copyright", "credits" or "license" for more information. .... """Send 'input' (string) to the...
2
by: eric_jin | last post by:
i called function show() in a c# webservice //show.m function ans=show(x) ans=x; it works; but when i try to call add(),it breaks; //add.m
1
Steel546
by: Steel546 | last post by:
I want to understand why certain functions do certain things. Such as in a linked list, I'm looking for the source code behind the list.at(), or list.resize() functions. cplusplus.com only gives...
0
by: kepbem | last post by:
I am new to this forum, i have created this database, however the timetables will not reset themselves. They have done previously, can anyone help? Option Compare Database Option Explicit ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.