Question Pulling Wrong info from array

jegriffin

New member
Joined
Nov 2, 2012
Messages
1
Programming Experience
Beginner
I am having a couple of problems. First, when I enter a number into the text box and hit enter It prints the number 12 times in the listbox. I only need one at a time. The other problems are the low price and average price. Can't get them to calculate properly. If anyone can help me figure this out, I would greatly appreciate it. Thanks!

Option Strict On
Public Class gasPrice
    Dim priceArray(11) As Decimal
    Dim numprice As Decimal = 0
    Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click

        For i = 0 To priceArray.GetUpperBound(0)
            priceArray(i) = Convert.ToDecimal(priceTextBox.Text)
            priceListBox.Items.Add(priceTextBox.Text)
        Next
        priceTextBox.Clear()

    End Sub

    Private Sub highButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles highButton.Click
        Dim highest As Decimal = 0
        Dim highmonth As Integer = 0

        For index As Integer = 0 To priceArray.GetUpperBound(0)
            If priceArray(index) > highest Then
                highest = priceArray(index)
                highmonth = index + 1
            End If
        Next
        resultLabel.Text = "The highest price is " & highest & " in month " & highmonth
    End Sub

    Private Sub lowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lowButton.Click
        Dim lowest As Decimal = 0
        Dim lowmonth As Integer = 0

        For index As Integer = 0 To priceArray.GetUpperBound(0)
            If priceArray(index) < lowest Then
                lowest = priceArray(index)
                lowmonth = index + 1
            End If
        Next
        resultLabel.Text = "The lowest price is " & lowest & " in month " & lowmonth
    End Sub

    Private Sub avgButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles avgButton.Click
        Dim average As Decimal = 0
        Dim total As Decimal = 0

        For i = 0 To priceArray.GetUpperBound(0)
            total += priceArray(i)
        Next
        'average = CLng(total) \ 11
        resultLabel.Text = "The Average Price is: " & average
    End Sub
End Class

 
Last edited by a moderator:
Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click

For i = 0 To priceArray.GetUpperBound(0)
priceArray(i) = Convert.ToDecimal(priceTextBox.Text)
priceListBox.Items.Add(priceTextBox.Text)
Next
priceTextBox.Clear()

End Sub
1. on every enterButton.Click event you run over ALL the array's members and set their value to priceTextBox.Text (look for a free slot, get a ref and enter the val once)
2. on every
enterButton.Click event you enter the value to priceTextBox.Text 12 times to priceListBox.Items (get that line out of the loop)

_________________________________________________________________________________________________________________________________________________________________________________

Private Sub lowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lowButton.Click
Dim lowest As Decimal = 0
Dim lowmonth As Integer = 0

For index As Integer = 0 To priceArray.GetUpperBound(0)
If priceArray(index) < lowest Then
lowest = priceArray(index)
lowmonth = index + 1
End If
Next
resultLabel.Text = "The lowest price is " & lowest & " in month " & lowmonth
End Sub

3 on declaration you set lowest as zero, set it to the first val of proceArray(1)
_________________________________________________________________________________________________________________________________________________________________________________

Private Sub avgButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles avgButton.Click
Dim average As Decimal = 0
Dim total As Decimal = 0

For i = 0 To priceArray.GetUpperBound(0)
total += priceArray(i)
Next
'average = CLng(total) \ 11
resultLabel.Text = "The Average Price is: " & average
End Sub


4 loose the comma "'average = CLng(total) \ 11"
5 change 11 to arrays length (someday its size might change)
 
Last edited:
The first issue is because you have a loop in the Click event handler of your enterButton. The upper bound of your array is 11 and that loop goes from 0 to that upper bound. As such, you setting all 12 elements to the value the user just entered. If the user enters 12 values, as I assume is the intention, then you will set all 12 elements all 12 times, so your array is going to end up containing just the last value entered in all 12 elements. If you only want to add the value once then get rid of the loop. Loops are for doing things multiple times. If you want to keep track of what element in the array set each time then you can use the number of items in the ListBox to work that out.

That last point leads to a question though: why use an array and a ListBox? You really shouldn't be maintaining two copies of the data. If you want to display the data to the user then you should maintain a list and bind that to the ListBox, so there's only one copy of the data to maintain.

With regards to calculating the lowest value, the issue is that you're initialising your 'lowest' variable to zero. Assuming that they're all positive, how are you going to detect the lowest value when all values are higher than your initial value? You need to initialise 'lowest' to a value that you know will be higher than all the values in the list. As your array is type Decimal, that would mean using Decimal.MaxValue.

As for calculating the average, have you actually read your own code? You initialise the 'average' variable to zero and then never assign anything else to it, so how is that supposed to work. How do you calculate an average? You divide the sum of all the values by the number of values, right? How many values in your array? Wouldn't the array itself tell you that? I can tell you that it's not 11. Also, given that you're working with Decimals, why exactly are you trying to convert the 'total' to a Long in that line that's commented out? Also, why use integer division when the values themselves aren't Integers?
 
Back
Top