4 Replies - 372 Views - Last Post: 16 July 2014 - 11:20 AM Rate Topic: -----

#1 stinkytotoro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 31-May 14

using for loops to determine highest above average

Posted 15 July 2014 - 08:47 PM

Ask the user to enter a number of scores one at a time and record the scores into the list of scores. See the figure below. When the Calculate button is clicked, the average score is calculated, the number of scores above the average is counted, and the scores above the average are added to the list box on the right.
Attached Image

   Private Sub btnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecord.Click
        lstListScores.Items.Add(txtEnter.Text)
        txtEnter.Clear()
        txtEnter.Focus()
    End Sub

    Private Sub btnAvg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAvg.Click
        Dim sum As Double = 0
        Dim avg As Double = 0
        Dim high As Double = 0
        If lstListScores.Items.Count > 0 Then
            For i As Integer = 0 To lstListScores.Items.Count - 1
                sum += CDbl(lstListScores.Items(i))
                avg = sum / lstListScores.Items.Count

                If CDbl(lstListScores.Items(i)) >= avg Then
                    high = CDbl(lstListScores.Items(i))
                    lstScoresAbove.Items.Add(high)
                End If
            Next
            txtAvg.Text = avg.ToString("N")

        End If
    End Sub
End Class



I cannot figure out how to display the numbers above average in the second listBox and cannot figure out how to count how many of them there are, any help

Is This A Good Question/Topic? 0
  • +

Replies To: using for loops to determine highest above average

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3479
  • View blog
  • Posts: 11,825
  • Joined: 12-December 12

Re: using for loops to determine highest above average

Posted 15 July 2014 - 10:09 PM

Quote

I cannot figure out how to display the numbers above average in the second listBox

Doesn't your code already do this? If not, what does it do?

You are already summing and averaging, why is counting so different? What have you tried?
This is your code isn't it?
Was This Post Helpful? 0
  • +
  • -

#3 stinkytotoro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 31-May 14

Re: using for loops to determine highest above average

Posted 16 July 2014 - 08:56 AM

 If CDbl(lstListScores.Items(i)) >= avg Then
	                    high = CDbl(lstListScores.Items(i))
	                    lstScoresAbove.Items.Add(high)
	                End If




this is what I am trying to use to put the scores that are above average into the second list box and it doesnt work
the picture I uploaded is what my code should be doing
when I input numbers I get them in the first list box, I am able to get the average
but I cannot figure out how to record the numbers above average to get them into the second list box

This post has been edited by andrewsw: 16 July 2014 - 10:25 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 stinkytotoro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 31-May 14

Re: using for loops to determine highest above average

Posted 16 July 2014 - 11:15 AM

    Private Sub btnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecord.Click
        lstListScores.Items.Add(txtEnter.Text)
        txtEnter.Clear()
        txtEnter.Focus()
    End Sub

    Private Sub btnAvg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAvg.Click
        Dim sum As Double = 0
        Dim sumabove As Double = 0
        Dim high As Double = 0
        Dim avg As Double = 0
        If lstListScores.Items.Count > 0 Then
            For i As Integer = 0 To lstListScores.Items.Count - 1
                sum += CDbl(lstListScores.Items(i))
                avg = sum / lstListScores.Items.Count
                For k = avg To 100
                    If CDbl(lstListScores.Items(i)) > avg Then
                        lstScoresAbove.Items.Add(i)
                    End If
                    sumabove = CDbl(lstScoresAbove.Items.Count)
                Next

            Next
            txtAvg.Text = avg.ToString("N")
            txtAboveAvg.Text = sumabove.ToString("N")
        End If
    End Sub
End Class



updated, still trying to figure out how to get the numbers above average into the list box so I can them count them
Was This Post Helpful? 0
  • +
  • -

#5 Oppemaj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 89
  • Joined: 15-November 13

Re: using for loops to determine highest above average

Posted 16 July 2014 - 11:20 AM

This is how you could do it:

 Dim counter As Integer = 0
        lstScoresAbove.Items.Clear()
        For Each number As Double In lstListScores.Items
            If number > avg Then
                counter += 1
                high = number
                lstScoresAbove.Items.Add(high)
            End If
        Next
        txtAboveAverage.Text = counter.ToString

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1