1

I'm trying to find the count of the digits, the lowest digit, the highest digit, and the average of all the digits in a string.

This is what I was able to come up with so far

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer = 0
        Dim minimumInt As Integer
        Dim maximumInt As Integer
        Dim averageInt As Single
        For Each c As Char In TextBox1.Text
            If Char.IsDigit(c) Then
                i = i + 1
                If (Asc(c) < minimumInt) Then
                    minimumInt = Asc(c)
                ElseIf (Asc(c) > maximumInt) Then
                    maximumInt = Asc(c)
                End If
                averageInt = averageInt + Asc(c)
                averageInt = averageInt / i
            End If
        Next
        MessageBox.Show("Total Digits: " & i & ControlChars.CrLf & "Average Number: " & averageInt & ControlChars.CrLf & "Minimum Number: " & minimumInt & ControlChars.CrLf & "Maximum Number: " & maximumInt)
    End Sub
End Class

I believe the issue is involving converting Chars to Integers.

For example, when TextBox1 contains "X13210AS", I get this http://i.imgur.com/5aaOWC0.png

When I should be getting an average of 1.4 and highest digit 3.

Any ideas on how to fix this while still using the majority of my code?

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178

3 Answers3

1

The first issue is with the ASCII conversion. "0" char is having value of 48.

The second issue is with your averaging, which should be done after you get all numbers instead of each loop. And beware of division by 0!

To get it right, you have to do this:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer = 0
        Dim minimumInt As Integer
        Dim maximumInt As Integer
        Dim averageInt As Single
        Dim intVal As Integer 'only use this
        For Each c As Char In TextBox1.Text
            If Char.IsDigit(c) Then
                i = i + 1
                intVal = Asc(c) - 48 'count this once, note -48 here is to convert ASCII rep to integer value
                If (intVal < minimumInt) Then
                    minimumInt = intVal
                ElseIf (intVal > maximumInt) Then
                    maximumInt = intVal
                End If
                averageInt += intVal
            End If
        Next
        If i <> 0 Then 'beware of 0
            averageInt = averageInt / i `put this outside
        End If
        MessageBox.Show("Total Digits: " & i & ControlChars.CrLf & "Average Number: " & averageInt & ControlChars.CrLf & "Minimum Number: " & minimumInt & ControlChars.CrLf & "Maximum Number: " & maximumInt)
    End Sub
End Class

And you will get this:

enter image description here

Ian
  • 30,182
  • 19
  • 69
  • 107
0

Asc(c) returns the ASCII value of the digit character, so e.g. '0' has an ASCII value of 48.

Assuming your goal is to ignore non-digits and average the rest, you will almost get you the correct average of you adjust for 0 = 48, 1 = 49, etc. You need to do this step

averageInt = averageInt / i

after the loop is done.

Your minimum value is 0 because minimumInt starts as 0. Start with minimumInt = Asc('9') or a similar high value.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • So from my understanding, I would have to put `Asc(c - 48)`? but that doesn't work and when I put the - 48 outside of the conversion, I get more jibberish – IdkHowToCodeAtAll Jan 27 '16 at 05:32
  • No, `Asc(c) - 48`, or perhaps to be more clear `Asc(c) - Asc('0')`. Also note I edited my question just now, pointing out that the final step of the average calculation needs to happen outside of the loop. – Eric J. Jan 27 '16 at 05:33
  • One lasttt question, sorry! But why do you put the numbers inside single quotes? When I do, I end up getting comments, are single quotes the same as Double quotes in vb.net? – IdkHowToCodeAtAll Jan 27 '16 at 05:35
  • @IdkHowToCodeAtAll Single quote is for character writing in VB.Net, thus 0 is integer (having value of 0) but "0'" is character (having value of 48) – Ian Jan 27 '16 at 05:37
  • I'm a C# guy... character constants are in single quotes. I guess it would be "0"C in VB.Net http://stackoverflow.com/questions/3374842/how-do-you-declare-a-char-literal-in-visual-basic-net – Eric J. Jan 27 '16 at 05:37
  • You guys are the best. – IdkHowToCodeAtAll Jan 27 '16 at 05:39
0

Using regex and List

Imports System.Text.RegularExpressions

you can do it in a simple way

TextBox1.Text = "X13210AS"
Dim lstnums As List(Of Integer) = Regex.Replace(TextBox1.Text, "[^\d]", ""). _
                                  Select(Function(x) Val(x)).ToList
Dim maxnum, minnum As Integer
Dim avg As Double

maxnum = lstnums.Max()
minnum = lstnums.Min()
avg = lstnums.Average
MessageBox.Show("Total Digits: " & Val(lstnums.Count) & ControlChars.CrLf & "Average Number: " & avg & ControlChars.CrLf & "Minimum Number: " & minnum & ControlChars.CrLf & "Maximum Number: " & maxnum)

[^\d] is the pattern to match integer in Textbox1

Vivek S.
  • 19,945
  • 7
  • 68
  • 85