Search code examples
vb.netstringcharstring-function

Find average of all digits in string


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?


Solution

  • 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