|
-
Feb 27th, 2013, 12:43 PM
#1
Thread Starter
New Member
Grouping numbers with arrays
Hi guys!
Stuck on a bit of homework and hoping someone here could point me in the right direction.
This is the first question I've done (and It works fine btw!),
basically what it's doing is generating 100 random numbers, and then dividing each number by 10 to get a 1 digit number, this number is then used to count how many numbers per group are generated (for example, numbers between 0-9,10-19,20-29...etc) and then displaying them in a list box
Code:
Private Sub btn_generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_generate.Click
Dim generator As Random = New Random(DateTime.Now.Millisecond)
Dim ageCount(0 To 99) As Short
Dim ageGroupNo As Short
Dim Age As Short
lst_output.Items.Clear()
lst_output.Items.Add("Age:" + vbTab + "Count:")
For i = 0 To 99 - For 100 iterations
Age = generator.Next(0, 100) - Generate a random num between 0-99
ageGroupNo = Age \ 10 - Divide number by 10
ageCount(ageGroupNo) += 1 - Increment Group
Next
Dim groups As New List(Of String)
For i = 0 To 9 - Generates group titles (0-9, 10-19...etc)
groups.Add(CStr(i * 10) + "-" + CStr((i * 10) + 9))
Next
For i = 0 To 9 - Displays data
lst_output.Items.Add(CStr(groups(i)) + vbTab + CStr(ageCount(i)))
Next
End Sub
so that's all well and good, but now the challenge
do the same thing but with 100 Decimal numbers between 1.0 and 2.75.
Code:
Private Sub btn_generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_generate.Click
Dim generator As Random = New Random(DateTime.Now.Millisecond)
Dim heightCount(0 To 99) As Decimal
Dim heightGroupNo As Decimal
Dim Height As Decimal
lst_output.Items.Clear()
lst_output.Items.Add("Height:" + vbTab + "Count:")
For i = 0 To 99 - For 100 iterations
Generate:
Height = Math.Round(generator.NextDouble() * 2.75, 2) - Random Num no greater than 2.75
If Height < 1.0 Then - Make sure number is at least 1.0
GoTo Generate
End If
heightGroupNo = (Height / 0.9) - THIS is the problem part
heightCount(heightGroupNo) += 1
Next
For i = 0 To 8
lst_output.Items.Add(CStr(heightCount(i)))
Next
End Sub
I need the generated decimals to be broken into 9 groups
Code:
< 1.20
1.20 - 1.39
1.40 - 1.59
1.60 - 1.79
1.80 - 1.99
2.00 - 2.19
2.20 - 2.39
2.40 - 2.59
>2.60
I've tried converting the decimals to whole numbers and then dividing by 9, but nothing seems to be getting me the desired results, I tried a select/case statement, which worked but obviously inefficient as hell,
I know there's a better way to do it then that!
I get the feeling I'm missing something obvious and perhaps a fresh pair of eyes tomorrow morning will do the trick, but as it stands I'm pulling my hair out :P
thanks for your time, hope you can assist!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|