Bill Stack
Bill Stack

Reputation: 67

Visual Basic - Counting the occurrence of numbers from a random array

I need to use arrays and strings. The program should just search for 1 number which the user will enter (0-9). Here is my code below. It uses an array to get a random number, and it's testing the occurrences for each number. However it's not working, but I don't need it to test for every number anyway.

The program must only test for the 1 number that the user requests. So if the random number that's generated is say.. '7417', and user user has chosen '7', then the program will report back there have been two sevens. I'm going to use a textbox 'txtEnter' to get the users number to search for. Can anyone help me? Thanks!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Randomize()
  Dim ArrayNum(1) As Integer
  ArrayNum(0) = Int(Rnd() * 100000)

  lblDisplayNumber.Text = ArrayNum(0)

  Dim num As Integer
  txtEnter.Text = num
  Dim counts = From c In num.ToString()
               Group By c Into Group
               Select DigitGroup = New With {.Count = Group.Count(),
                                             .Digit = c, .Group = Group}
               Order By DigitGroup.Count Descending
               Select String.Format("There are {0} number {1}'s found.",
                                     DigitGroup.Count, DigitGroup.Digit)
  Dim message = String.Join(Environment.NewLine, counts)
End Sub

Upvotes: 2

Views: 4992

Answers (3)

Andrew Morton
Andrew Morton

Reputation: 25013

The solutions presented earlier seem a bit complicated if you just need to find how many times the entered number is in the random digits.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ' create the data
    Dim nDigits As Integer = 5
    Dim digits(nDigits - 1) As String
    For i = 0 To nDigits - 1
        digits(i) = CInt(Rnd() * 10).ToString
    Next

    ' show the data
    TextBox1.Text = String.Join("", digits)

    Dim inp = InputBox("Enter a number from 0 to 9")

    ' count the occurrences of the entered number
    Dim nFound = digits.Count(Function(d) d = inp)

    MsgBox(String.Format("There are {0} occurrences of {1}.", nFound, inp))

End Sub

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460108

If you want to use Linq, it's short and readable:

Dim counts = From c In num.ToString()
             Group By c Into Group
             Select DigitGroup = New With {.Count = Group.Count(), .Digit = c, .Group = Group }
             Order By DigitGroup.Count Descending
             Select String.Format("There are {0} number {1}'s found.",
                                   DigitGroup.Count, DigitGroup.Digit)
Dim message = String.Join(Environment.NewLine, counts)

Update: Here is a complete sample which shows the result of the entered number and a summary:

Dim rnd As New Random()
Dim randomInt = rnd.Next(0, 100000)
lblDisplayNumber.Text = randomInt.ToString()

Dim num As Integer
If Int32.TryParse(txtEnter.Text, num) AndAlso num >= 0 AndAlso num < 10 Then
    Dim counts = From c In randomInt.ToString()
             Group By c Into Group
             Select DigitGroup = New With {.Count = Group.Count(), .Digit = c, .Group = Group}
             Order By DigitGroup.Count Descending, DigitGroup.Digit
    Dim numCount = counts.FirstOrDefault(Function(grp) grp.Digit.ToString() = num.ToString())
    If numCount IsNot Nothing Then
        Dim numMessage = String.Format("There are {0} number {1}'s found.", numCount.Count, num)
        MessageBox.Show(numMessage)
    Else
        MessageBox.Show("Number does not contain " & num)
    End If
    Dim summaryCount = From grp In counts
                       Select String.Format("There are {0} number {1}'s found.", grp.Count, grp.Digit)
    Dim summaryMessage = String.Join(Environment.NewLine, summaryCount)
    MessageBox.Show("Summary:" & Environment.NewLine & summaryMessage)
Else
    MessageBox.Show("Please enter a number between 0 and 9.")
End If

Upvotes: 2

Doc Brown
Doc Brown

Reputation: 20044

Your code has several issues, but to give you an answer directly to what is missing:

you should convert your random number to a string first

 Number= Int(Rnd() * 100000) 
 NumberString = Number.ToString()

convert that string to an array of characters

 DigitArray = NumberString.ToCharArray()

then loop over all characters of the array and convert each character c back to an integer

 For Each c As Char In DigitArray
     num = c - "0"C`

Then use that num as index for your counts array

     count(num)+=1       

instead of that horrible switch statement. I let you figure out the correct Dimstatements for those variables.

Upvotes: 1

Related Questions