These are the instructions for the assignment. I have done everything but I'm stuck on number 8.

1.Write a function that takes in a string of numbers and returns the corresponding array of those numbers in the Single data type.
2.Write another function that takes an array of Single and returns the min element of that array using code that you write (not built-in functions).
3.Write a function as in 2 but returns the max.
4.Write a function as in 3 but returns the average.
5.Write a function that takes in an array of Single and uses the function of 4 and returns how many of the array elements are above the average.
6.All of the functions above should be in a module file.
7.On a form, add buttons to: Enter Data, Min, Max, Average, Num above Average. Write the required event handler for those buttons.
8.The handler for Enter Data uses a Loop on Inputbox to get a "long" string of numbers separated by a comma. This string will be passed the to the first function to get an array of numbers. This array should be saved into an array that is visible to the handlers below.
9.Write the handlers for the remaining buttons so each handler shows the value the handler is expected to show. E.g. Min shows the minimum number from the array.

Module Module1
    Public Function inputData(ByVal a As String) As Single()
        Dim i As Integer
        Dim arr(a.Length) As Single
        Dim curr As String = ""
        Dim count As Integer
        Dim firstcomma As Boolean = True
        Try

            For i = 0 To a.Length - 1
                If a.Substring(i, 1) <> "," Then
                    curr = curr & a.Substring(i, 1)
                Else
                    If firstcomma = False Then
                        arr(count) = CSng(curr)
                        count += 1
                    Else
                        firstcomma = False

                    End If

                End If
            Next

        Catch ex As InvalidCastException

        End Try
        Return arr
    End Function
    Public Function minimum(ByVal arr As Integer()) As Single
        Dim i As Integer
        Dim min As Integer
        min = arr(0)
        For i = 0 To arr.GetUpperBound(0)
            If min > arr(i) Then
                min = arr(i)
            End If
        Next i
        Return min
    End Function
    Public Function maximum(ByVal arr As Integer()) As Single
        Dim i As Integer
        Dim max As Integer
        max = arr(0)
        For i = 0 To arr.GetUpperBound(0)
            If max < arr(i) Then
                max = arr(i)
            End If
        Next i
        Return max
    End Function
    Public Function average(ByVal arr As Integer()) As Single
        Dim total As Integer
        Dim i As Integer
        For i = 0 To arr.GetUpperBound(0)
            total = total + arr(i)

        Next i
        Return CSng(total / arr.GetUpperBound(0) + 1)
    End Function
    Public Function NumAboveAve(ByVal arr As Integer()) As Single
        Dim counter As Integer
        For i = 0 To arr.GetUpperBound(0)
            If arr(i) > average(arr) Then
                counter += 1
            End If
        Next
        Return counter
    End Function
End Module


*Form*

Dim arr() As Single
    Dim StrVar As String



    Private Sub BtnData_Click(sender As System.Object, e As System.EventArgs) Handles BtnData.Click
        Dim usernum As String
        usernum = InputBox("Enter Data", "Array")
        StrVar = StrVar & "," & usernum
        StrVar.Trim(CChar(","))
        arr = inputData(StrVar)
        'do While 
    End Sub


    Private Sub btnMin_Click(sender As System.Object, e As System.EventArgs) Handles btnMin.Click
        MessageBox.Show(StrVar)
    End Sub

    Private Sub BtnMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMax.Click
        MessageBox.Show(StrVar)
    End Sub

    Private Sub BtnAve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAve.Click
        MessageBox.Show(StrVar)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(StrVar)
    End Sub
End Class

Recommended Answers

All 5 Replies

You are so close that it is not even funny.

You will have to devise a method of determining if the user is finished entering data. For example, maybe a next and finished button?

After the user enters a VALID number in the text box, add it to a class level variable string, then delimit the string with the "," char.

But do not add the "," at the very end of the string! Hint (Use the next to add to to the string. Use the finish to add the last number.)

Again, you are so close!

Hi joester007 or Begginnerdev... i have a same problem and im stuck on it.. can you please help me?

@TheRealSai you should start your own question. Remember to include the code you've done and details about where exactly the code is failing.

Did you ever figure out what to put in the Do While loop? I know it should look something like this:

Do While strData <> ""
    strData = InputBox("Enter Data", "Array")
    TotalS = TotalS & strData  'if S <> ""
    'code
Loop

But what kind of variable is TotalS? i.e. What data type? Is this the array? And how do you pass it to the first function (questions 1)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.