How do I see the largest number of a set of numbers as well as their mean?

0

I need to create the code for a user to enter with an indeterminate number of numbers and then see the largest of the numbers and make their average.

Each entry is made by clicking on the Confirm button (done in design ) < I then created a variable y that will accumulate the values.

When the "major" button is pressed, it places the largest of the numbers on another textbox made to the result. The average button should give the average (also in the textbox of the result) of the numbers that have entered so far.

It's a more or less simple problem, I think, but it needed a little help. I have attached what I have done in code.

Public Class Form1
    Dim y As Integer
    Private Sub btnconfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconfirm.Click
        Dim numeros(txtboxnum.Text) As Integer

        If IsNumeric(txtboxnum.Text) Then
            y = y + 1
            txtboxnum.clear()
        Else
            MessageBox.Show("Atenção, introduza um NÚMERO")
            Return
            txtboxnum.Focus()
        End If
    End Sub

    Private Sub btnmaior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmaior.Click
        Dim maior As String
        For c = 0 To txtboxnum.Text.Length - 1
            If txtboxnum.Text(y) > maior Then
                maior = txtboxnum.Text(y)
            End If
        Next
        txtboxresult.Text = maior

    End Sub

    Private Sub btnmedia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmedia.Click
        For c = 0 To txtboxnum.Text.Length - 1
        Next
    End Sub

    Private Sub btnnovosnums_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnovosnums.Click
        txtboxnum.Clear()
        txtboxresult.Clear()
    End Sub
End Class
    
asked by anonymous 14.06.2015 / 18:31

1 answer

2

In fact, the variable y is not a set of numbers , but rather a variable you are using to add the Textbox entries, you need to store the numbers in arrayList .

Dim numeros As New ArrayList

For the largest number of array , use function Math.Max :

Dim maiorNumero As Integer = 0
Dim total As Integer = 0

For Each numero As Integer In numeros
     total += numero
     maiorNumero = Math.Max(maiorNumero, numero)
Next

To get the mean, you must add the array numbers and divide it by the number of numbers:

Dim media As Integer = total / numeros.Count 

Note : You must use imports System.Collections

View demonstração

    
14.06.2015 / 19:38