Question Counting number groups with array.

DosBoss

Member
Joined
Sep 11, 2012
Messages
9
Programming Experience
Beginner
Hi all!

So I've come across a homework problem that's leaving me rather frustrated,
I've done the first part that entails I generate a list of 100 random numbers, then divide each number by 10 to get a single digit result, I then use this digit to find out how many lots of number groups exit, allow me to elaborate with an example.

VB.NET:
        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 [COLOR=#0000cd]-for 100 iterations[/COLOR]
            Age = generator.Next(0, 100) [COLOR=#0000cd]-generate a random number between 0-99[/COLOR]
            ageGroupNo = Age \ 10 [COLOR=#0000cd]-divide that number 10 [/COLOR]
            ageCount(ageGroupNo) += 1 [COLOR=#0000cd]- increment the counter for that number group[/COLOR]
        Next


        Dim groups As New List(Of String)
[COLOR=#0000cd]-This simply adds the groups to a list box(0-9, 10-19,20-29..etc[/COLOR])
        For i = 0 To 9
            groups.Add(CStr(i * 10) + "-" + CStr((i * 10) + 9))
        Next

        For i = 0 To 9
            lst_output.Items.Add(CStr(groups(i)) + vbTab + CStr(ageCount(i))) [COLOR=#0000cd]-Output data[/COLOR]
        Next

So as you can see, this code will take our 100 random numbers and count how many fit into each number group,
that's all well and good, but now the challenge!

I essentially need to do the same thing, but instead of numbers between 0-99, I'm generating numbers between 1.0-2.75...yup decimals!

Here's my code thus far

VB.NET:
        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()

        For i = 0 To 99
Generate:
            Height = Math.Round(generator.NextDouble() * 2.75, 2) [COLOR=#0000cd]-Generate number no greater than 2.75[/COLOR]

            If Height < 1.0 Then [COLOR=#0000cd]- Make sure number is at least 1.0[/COLOR]
                GoTo Generate
            End If

            heightGroupNo = (Height / 9) [COLOR=#0000cd]-THIS is what I need to work out I believe[/COLOR]

            heightCount(heightGroupNo) += 1 [COLOR=#0000cd]- increment the counter for that number group[/COLOR]
        Next

        For i = 0 To 8
            lst_output.Items.Add(CStr(heightCount(i))) [COLOR=#0000cd]- Output data[/COLOR]
        Next

The number groups for these numbers need to be as follows

VB.NET:
1.00 - 1.19
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 - 2.75

I've tried a few methods, including converting the numbers into whole numbers and then dividing by 9, but alas nothing so far is giving me the desired results, perhaps some fresh eyes tomorrow morning will yield better results, even so, if someone could give me a shove in the right direction, I'd really appreciate it!

thanks for your time.
 

DosBoss

Member
Joined
Sep 11, 2012
Messages
9
Programming Experience
Beginner
Thanks to the kind help of some members of another forum I was able to get it going :D
Here's the final result for anyone interested

VB.NET:
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 ranges(,) As Decimal = {{1D, 1.19D}, {1.2D, 1.39D}, {1.4D, 1.59D}, {1.6D, 1.79D}, {1.8D, 1.99D}, {2D, 2.19D}, {2.2D, 2.39D}, {2.4D, 2.59D}, {2.6D, 2.75D}}
        Dim heightCount(0 To 99) As Decimal
        Dim heightGroupNo As Decimal
        Dim Height As Decimal
        Dim totalHeight As Decimal

        lst_output.Items.Clear()
        lst_output.Items.Add("Height:" + vbTab + vbTab + "Count:")

        For i = 0 To 99
            Do
                Height = CDec(Math.Round(generator.NextDouble() * 2.75, 2))
            Loop While Height < 1D

            totalHeight += Height

            heightGroupNo = getIndex(ranges, Height)

            heightCount(heightGroupNo) += 1
        Next

        For i = 0 To 8
            lst_output.Items.Add(If(i = 0, "< 1.20", If(i = 8, "> 2.60", String.Format("{0} - {1}", ranges(i, 0), ranges(i, 1)))) & vbTab & vbTab & CStr(heightCount(i)))
        Next

        txt_average.Text = CStr(FormatNumber(totalHeight / 100, 2) + "M")

    End Sub

    Private Function getIndex(ByVal d(,) As Decimal, ByVal value As Decimal) As Integer
        For x As Integer = 0 To d.GetUpperBound(0)
            If value >= d(x, 0) And value <= d(x, 1) Then Return x
        Next
        Return -1
    End Function
 
Last edited:
Top Bottom