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

Asked

Viewed 611 times

0

I need to create the code for a user to enter an undetermined number of numbers and then see the largest number and average it.

Each entry is made by clicking on button "confirm" (done in the design of Form) that accumulates the values entered and then clears them, for this I created a variable y that accumulates the values.

When the button "larger" is pressed, put on another textbox made for the result, the largest of the numbers. The button "medium" should give the average (also in the textbox of the result) of the numbers entered so far.

It is a problem more or less simple I think, but I needed a little help. I enclose what I did of 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
  • Crisp, you’ve specified everything you have to do, but haven’t clarified your real difficulty.

  • @Fabricio my difficulty is how I do to see which is the largest number of a set of numbers introduced by the user and make the average of them

  • You should wear a array to store the numbers, not add them up.

1 answer

2


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

Dim numeros As New ArrayList

To get the most number of a array, use the 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 average, you must sum up the numbers of the array and divide it by the amount of numbers:

Dim media As Integer = total / numeros.Count 

Note: It is necessary to use imports System.Collections

See demonstração

  • @crispypedro_ You are adding the input of textbox in array?

  • How so ? should I do: Dim numbers(txtboxnum.text) As Arraylist ?

  • @crispypedro_ If possible delete the previous comments. You looked at the example I posted? The way you’re doing it, it’s wrong.

  • I did, but in your demo you’re working with your Application Console I don’t know if you can make the mistake. for the larger button I put the code "For Each numero..." and for the medium button I put the "Dim media As Integer = ..."

  • @crispypedro_ See this example with forms: http://pastebin.com/Um9DdZSL Watch the comments! anything comment if it doesn’t work or if possible, mark the response with the sign below the score.

  • finally works, sorry to Thank you! Thank you!!!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.