5 Replies - 19595 Views - Last Post: 06 March 2011 - 07:05 AM Rate Topic: -----

#1 FractuRe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-March 11

VB getting an average of an array, VS 2008

Posted 05 March 2011 - 07:46 PM

Hey guys, trying to give an average of an array in VB MVS2008. I tried using a sample code online but the averages it gives me are completely off. If I enter three ages (20) it gives me an average of 0.6, instead of 20. Here's the code:

Spoiler


Is This A Good Question/Topic? 0
  • +

Replies To: VB getting an average of an array, VS 2008

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: VB getting an average of an array, VS 2008

Posted 05 March 2011 - 07:57 PM

Can't you just use Ages.Average?
Was This Post Helpful? 0
  • +
  • -

#3 FractuRe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-March 11

Re: VB getting an average of an array, VS 2008

Posted 05 March 2011 - 08:31 PM

View PostCharlieMay, on 05 March 2011 - 07:57 PM, said:

Can't you just use Ages.Average?

Okay, so I changed the average ages button code to:

    Private Sub btnAveAge_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnAveAge.Click 'Button to average the all ages in the Ages array
        txtboxAveAges.Text = Ages.Average
    End Sub


Now when I enter three values of 20 it averages it to 0.594059405940594. Any idea why this is happening?
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: VB getting an average of an array, VS 2008

Posted 05 March 2011 - 08:46 PM

OK, it's because you dim the array with 100 elements so regardless of how many you enter 100 is being used as the divisor for average. Even if you were using a for next to get each element it would still divide by 100.

You will need to redim the array as you go so as to make sure that the size of the array matches the entries you have entered.

An example would be
Private Sub btnAdd_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnAdd.Click    'Button to add names and ages to the related array
        Names(nCusts) = txtName.Text
        ReDim Preserve Ages(nCusts)
        Ages(nCusts) = txtAge.Text
        nCusts += 1
End Sub

I would also recommend that you ensure that what is entered into the txtAge textbox is a valid number because if a letter is entered you will receive an error. Look into TryParse for ensuring that the value is valid before adding it to the array.

This post has been edited by CharlieMay: 05 March 2011 - 08:48 PM

Was This Post Helpful? 1
  • +
  • -

#5 FractuRe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-March 11

Re: VB getting an average of an array, VS 2008

Posted 05 March 2011 - 08:58 PM

Worked. Thank you so much, sorry that I am a noob.
I'm reading a lot of VB self-help books to try and catch up to my college peers.
Was This Post Helpful? 0
  • +
  • -

#6 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 121
  • View blog
  • Posts: 666
  • Joined: 01-October 08

Re: VB getting an average of an array, VS 2008

Posted 06 March 2011 - 07:05 AM

IF an age of zero is not allowed you can do this without resizing the array


        Dim average As Double = (From n In Ages Where n > 0 Select n).Average


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1