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