Finding highest and lowest nummber in array?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 922 Views - Last Post: 06 April 2013 - 10:26 PM Rate Topic: -----

#1 Klooless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-April 13

Finding highest and lowest nummber in array?

Posted 05 April 2013 - 08:21 PM

I have entered the following code but it does not output the highest or lowest numbers in an array of 12 numbers.

Public Class fmrGasPrices
    Dim gasArray(11) As Double
    Dim i As Integer
   
    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        lstGasPrices.Items.Add(txtPriceEntered.Text)

        btnGetLow.Enabled = True
        btnGetHigh.Enabled = True
        txtPriceEntered.Clear()

    End Sub

    Private Sub btnGetLow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetLow.Click
        Dim lowPrice As Double = 0.0


        For Each price As Double In gasArray
            If price < lowPrice Then
                lowPrice = price
            End If
        Next

        lblResults.Text = Convert.ToString(lowPrice)
        btnGetLow.Enabled = False ' 
    End Sub

    Private Sub btnGetHigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetHigh.Click
        Dim highPrice As Double = 0.0

        For Each price As Double In gasArray
            If price > highPrice Then
                highPrice = price
            End If
        Next

    End Sub


End Class


This post has been edited by macosxnerd101: 05 April 2013 - 08:37 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Finding highest and lowest nummber in array?

#2 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 201
  • View blog
  • Posts: 706
  • Joined: 01-February 13

Re: Finding highest and lowest nummber in array?

Posted 05 April 2013 - 08:53 PM

Hi,
Your trying to loop threw gasArray and check for the prices but, you do not have anywhere in the program that you have assigned the numbers to the (gasArray).
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Finding highest and lowest nummber in array?

Posted 05 April 2013 - 08:59 PM

There is this little thing called List(Of T). It is a type specific collection and it has some nice extension functions that would make life simpler.

Dim myDoubles As New List(Of Double)
myDoubles.Add(0.1)
myDoubles.Add(0.9)
Dim max As Double = myDoubles.Max
Dim min As Double = myDoubles.Min

Was This Post Helpful? 1
  • +
  • -

#4 Klooless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-April 13

Re: Finding highest and lowest nummber in array?

Posted 05 April 2013 - 09:30 PM

Ironrazor - Thanks for the response but I am not quite sure how i assign the values to gasArray. i tried
lstGasPrices.Items.Add(gasArray(i))

but that enters the value from the text box to the list box and then adds an additional "0" value???
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Finding highest and lowest nummber in array?

Posted 05 April 2013 - 09:39 PM

Fills the first array element:
gasArray(0) = 1.0

Was This Post Helpful? 1
  • +
  • -

#6 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 201
  • View blog
  • Posts: 706
  • Joined: 01-February 13

Re: Finding highest and lowest nummber in array?

Posted 05 April 2013 - 09:41 PM

View PostKlooless, on 05 April 2013 - 11:30 PM, said:

Ironrazor - Thanks for the response but I am not quite sure how i assign the values to gasArray. i tried
lstGasPrices.Items.Add(gasArray(i))

but that enters the value from the text box to the list box and then adds an additional "0" value???


Its not very hard. You just need to assign each element of the array 0 - 11 the value of the textbox when you press btnEnter. I would say _HAWK_ has the best way to deal with this but, if you have to use an array then you can do it like this
    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        

        gasArray(i) = Val(txtPriceEntered.Text)
        i += 1

        btnGetLow.Enabled = True
        btnGetHigh.Enabled = True
        txtPriceEntered.Clear()

    End Sub


This post has been edited by IronRazer: 05 April 2013 - 09:43 PM

Was This Post Helpful? 1
  • +
  • -

#7 Klooless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-April 13

Re: Finding highest and lowest nummber in array?

Posted 05 April 2013 - 10:03 PM

Ironrazor thank you so much!!!

Hawk thank you as well. I am sure if I totally understood what I was trying to achieve I would have used your suggestion.

I will try and figure out how to disable the enter but after 12 amounts are entered. I thought I had it figured out but its doesnt seem to be working.

If i wanted to use a function to calculate the average yearly gas price would the correct way be to use a function to call the calculation?
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 201
  • View blog
  • Posts: 706
  • Joined: 01-February 13

Re: Finding highest and lowest nummber in array?

Posted 05 April 2013 - 10:19 PM

To disable the button you could just use an IF statment in the (btnEnter) event to see if i is equal to 11 and if it is then disable the button. For the average you can do it in a function or in a button event. It just depends on where and when you are suppose to get the average. Try what you can and if you need more help then come back and ask.
Was This Post Helpful? 1
  • +
  • -

#9 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 201
  • View blog
  • Posts: 706
  • Joined: 01-February 13

Re: Finding highest and lowest nummber in array?

Posted 05 April 2013 - 10:40 PM

Hey,
Forgot to mention that in your find lowest price button event you have (lowprice) set to 0.0 and when it is looping threw the array looking for a lower price it will never find one because they won`t be lower than 0. You will want to set (lowprice) equal to the 0 element of the gasPrice array. :D
Was This Post Helpful? 1
  • +
  • -

#10 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 108
  • View blog
  • Posts: 601
  • Joined: 01-October 08

Re: Finding highest and lowest nummber in array?

Posted 06 April 2013 - 10:35 AM

View Post_HAWK_, on 05 April 2013 - 10:59 PM, said:

There is this little thing called List(Of T). It is a type specific collection and it has some nice extension functions that would make life simpler.

Dim myDoubles As New List(Of Double)
myDoubles.Add(0.1)
myDoubles.Add(0.9)
Dim max As Double = myDoubles.Max
Dim min As Double = myDoubles.Min


So do arrays.

        Dim gasArray(11) As Double


        Dim highPrice As Double = gasArray.Max
        Dim lowPrice As Double = gasArray.Min
        Dim avgPrice As Double = gasArray.Average


This post has been edited by dbasnett: 06 April 2013 - 10:36 AM

Was This Post Helpful? 1
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Finding highest and lowest nummber in array?

Posted 06 April 2013 - 11:40 AM

Actually Array and List(of T) don't have .Min and .Max.

They appear to because they both implement the Interface IEnumerable(Of T)

.Min and .Max are extension methods implemented in
System.Linq.Enumerable module.

It easily to prove this by not referencing or importing System.Linq namespace, those extension are not available.

This post has been edited by AdamSpeight2008: 06 April 2013 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Finding highest and lowest nummber in array?

Posted 06 April 2013 - 12:19 PM

That may be but I don't like arrays - Redim this, Redim that, yuck - horrible looking code!
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Finding highest and lowest nummber in array?

Posted 06 April 2013 - 12:46 PM

Arrays are the basic collection, and nearly all of the other collection types use the internally. The cost of the resizing (a redim and copy of contents) is hidden in amortized times, at some point one of the .Add will take sufficiently longer as compared to the other times.
Was This Post Helpful? 0
  • +
  • -

#14 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Finding highest and lowest nummber in array?

Posted 06 April 2013 - 12:49 PM

I get that Adam - it's called a preference. I still use them in byte buffers. Just don't like them in these instances.
Was This Post Helpful? 0
  • +
  • -

#15 Klooless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-April 13

Re: Finding highest and lowest nummber in array?

Posted 06 April 2013 - 07:41 PM

Hi Guys, I am back here is my code now. I am trying to calculate the average of the array, Its gives me an error of an InvalidProgramException

Public Class fmrGasPrices
    Dim gasArray(11) As Decimal
    Dim i As Decimal
    Dim Idx As Integer
    Dim months As Integer = 12
    Dim highPrice As Decimal = gasArray.Max
    Dim lowPrice As Decimal = gasArray.Min
    Dim avgPrice As Decimal = gasArray.Average
    Dim total As Decimal
   
    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        lstGasPrices.Items.Add(txtPriceEntered.Text)

        gasArray(i) = Val(txtPriceEntered.Text)
        i += 1

        If Idx > gasArray(11) Then
            btnEnter.Enabled = False
        End If
      

        btnGetLow.Enabled = True
        btnGetHigh.Enabled = True
        txtPriceEntered.Clear()

    End Sub

    Private Sub btnGetLow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetLow.Click
        Dim lowPrice = gasArray(11)


        For Each price As Double In gasArray
            If price < lowPrice Then
                lowPrice = price
            End If
        Next

        lblResults.Text = Convert.ToString(lowPrice)
        btnGetLow.Enabled = False ' 
    End Sub

    Private Sub btnGetHigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetHigh.Click
        Dim highPrice = gasArray(11)

        For Each price As Double In gasArray
            If price > highPrice Then
                highPrice = price
            End If
        Next

        lblResults.Text = Convert.ToString(highPrice)
        btnGetHigh.Enabled = False ' 

    End Sub

    Private Sub btnGetAve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetAve.Click

        calculateAverage(total, months)

        displayavePrice(avgPrice)
    End Sub

    Function calculateAverage(ByVal prices As Decimal, ByVal months As Integer) As Decimal
        'Dim average As Decimal = 0D
        Dim total As Decimal = 0D
        avgPrice = Convert.ToDecimal(total * months)

        For gasArray(11) = 0 To 11
            avgPrice = total + (gasArray(11))
        Next gasArray(11)

        avgPrice = total / months

        Return avgPrice
    End Function

    Private Sub displayavePrice(ByVal avgPrice As Decimal)
        lblResults.Text = String.Format("{0:C}", avgPrice)
    End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2