locked
averaging numbers from an array RRS feed

  • Question

  • I am very new to VB and am working on making additions to someone
    else's code.  I'm working with data that has been imported in to
    arrays.  What I'd like to do is take one if the arrays and calculate
    the average of the first 15% of the values.  I'm pretty sure I need an
    if loop or a for loop of some kind that keeps adding number togethers
    until it's reached that 15% and then divides by the index, but I have
    no idea what the syntax of the code should be.  Can anyone help me
    out?
    Thanks!
     
    Thursday, September 27, 2007 6:25 PM

Answers

  • Here is a sample array with a length of 14.  15% of 14 is 2.1, which rounds out to 2 after being converted to an integer to use in the For loop.  The For loop would range from 0 to the length -1, which would include the first two values in this sample.  It uses an accumulator inside the loop and then averages the total after the loop ends, displaying the result in the label.

     

    Code Block

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim myarray() As Double = {3, 5, 7, 12, 45, 90, 34, 56, 2, 6, 71, 89, 25, 43}

    Dim arlen As Integer

    Dim tot, avg As Double

    arlen = Convert.ToInt32(myarray.Length * (15 / 100))

    For x As Integer = 0 To arlen - 1

       tot += myarray(x)

    Next

    avg = tot / arlen

    Label1.Text = avg.ToString

    End Sub

     

     

     

     

    Thursday, September 27, 2007 7:02 PM
  •  ljCMH wrote:
    I am very new to VB and am working on making additions to someone
    else's code.  I'm working with data that has been imported in to
    arrays.  What I'd like to do is take one if the arrays and calculate
    the average of the first 15% of the values.  I'm pretty sure I need an
    if loop or a for loop of some kind that keeps adding number togethers
    until it's reached that 15% and then divides by the index, but I have
    no idea what the syntax of the code should be.  Can anyone help me
    out?
    Thanks!
     

     

    Hi,

     

    The answer from user Solitaire above is better understood than this I think as She has used the

    variable name arlen to indicate the array length.

     

    The array length will be the upperbound value + 1.

    The upperbound value is given by Ubound( arrayName ) as in the code I hereby post below.

     

    I forgot about the length property.  Ah well, never mind.

    The method is similiar however.

     

    I answered this at the end of the other thread that you posted in.>>

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2201620&SiteID=1&mode=1

     

    Here it is again.>>

     

    0.15 = 15 percent.

     

     

    Code Block

     

     

    Public Class Form1

     

    'The next highlighted line should be on one line in your code window.>>>>

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    'This could be any number, I chose

    '123456 which is 123457 elements

    'including element zero.

    'Fill the array with some values.

    Dim anArrayOfNumbers(123456) As Double

    For count = 123456 To 0 Step -1

    anArrayOfNumbers(count) = 123456 - count

    Next

    Dim fifteenPerCent As Integer = 0

    ' 1 is added as imagine the array is numbers(99)

    ' then 99 + 1 = 100

    ' 15% of which is 0.15 * 100 = 15

    ' Element numbers(0) needs to be included.>> 

    fifteenPerCent = Int(0.15 * (UBound(anArrayOfNumbers) + 1 ))

     

    Dim total As Double = 0

    'Would do the 1st 15 elements if the array has 100 elements.>>

    For index As Integer = 0 To fifteenPerCent - 1

    total = total + anArrayOfNumbers(index)

    Next

    'One is added as elements zero,one,two and three

    'are four separate elements.

    MessageBox.Show("Average= " & (total / (fifteenPerCent + 1)).ToString)

    End Sub

    End Class

     

     

     

     

    Regards,

     

    John.

     

    Friday, September 28, 2007 2:37 PM

All replies

  • Here is a sample array with a length of 14.  15% of 14 is 2.1, which rounds out to 2 after being converted to an integer to use in the For loop.  The For loop would range from 0 to the length -1, which would include the first two values in this sample.  It uses an accumulator inside the loop and then averages the total after the loop ends, displaying the result in the label.

     

    Code Block

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim myarray() As Double = {3, 5, 7, 12, 45, 90, 34, 56, 2, 6, 71, 89, 25, 43}

    Dim arlen As Integer

    Dim tot, avg As Double

    arlen = Convert.ToInt32(myarray.Length * (15 / 100))

    For x As Integer = 0 To arlen - 1

       tot += myarray(x)

    Next

    avg = tot / arlen

    Label1.Text = avg.ToString

    End Sub

     

     

     

     

    Thursday, September 27, 2007 7:02 PM
  • Thanks!  I've done some tweaking to fit my code.  First My array is previously defined as  My array is defined as:

    RightPelTilt = RPelvisdata(0, Index - FirstRight)

     

    So the data it is reading has 3 components to it, this defines RightPelTilt as the 1st component in that data set with values ranging from the Index value to a previously defined FirstRight Value. I think I'm still having problems converting hovwer ... I have:

     

    Code Block

    Dim arlen As Integer

    Dim tot, AvgPelTilt As Double 'I'm getting an error here that there is a duplicate declaration


    arlen = Convert.ToInt32(RightPelTilt.Length * (15 / 100)) 'I'm getting an error here that an object is needed

    For Index = 0 To arlen - 1

       tot = tot + RightPelTilt(Index - FirstRight)           'I changed this from += because my version of VB wasn't recognizing it

    Next

    AvgPelTilt = tot / arlen

     

     

     

     

    Thursday, September 27, 2007 7:34 PM
  • >>Dim tot, AvgPelTilt As Double 'I'm getting an error here that there is a duplicate declaration

     

    That's probably because you already declared AvbPelTilt as an array, or declared it as a variable somewhere else.


    >>arlen = Convert.ToInt32(RightPelTilt.Length * (15 / 100)) 'I'm getting an error here that an object is needed

     

    If RightPelTilt is not an array, then it doesn't have a length.  Make sure you are using an array in that statement.

     

    The arlen variable is used as the terminating value in the For loop, with the control variable declared as an integer.  The formula can be computed to a fractional value and it needs to be converted into an integer or will generate a type-mismatch error.

     

    Thursday, September 27, 2007 9:16 PM
  •  

    Hmm, maybe I'm not using an array ... here are some other key steps in my code that come before this new calculation:

     

    Code Block

    'gets trajectory index in c3d file

    i39 = Trial.FindTrajectoryIndex("RPelvisAngles", 0)

     

    'defines variable as newly identified trajectory index

    Set RPelvisAngles = Trial.Trajectory(i39)

     

    'fills in data from trajectory objects to VBA variable array

    RPelvisdata = RPelvisAngles.GetPoints(FirstRight, LastRight)

     

    For Index = FirstLeft To LastLeft

    RightPelTilt = RPelvisdata(0, Index - FirstRight)

    Next Index

     

     

     

    Does that make sense?  So then I want to search within that RightPelTilt variable for the 15% and find the average.
    Friday, September 28, 2007 2:12 PM
  •  ljCMH wrote:
    I am very new to VB and am working on making additions to someone
    else's code.  I'm working with data that has been imported in to
    arrays.  What I'd like to do is take one if the arrays and calculate
    the average of the first 15% of the values.  I'm pretty sure I need an
    if loop or a for loop of some kind that keeps adding number togethers
    until it's reached that 15% and then divides by the index, but I have
    no idea what the syntax of the code should be.  Can anyone help me
    out?
    Thanks!
     

     

    Hi,

     

    The answer from user Solitaire above is better understood than this I think as She has used the

    variable name arlen to indicate the array length.

     

    The array length will be the upperbound value + 1.

    The upperbound value is given by Ubound( arrayName ) as in the code I hereby post below.

     

    I forgot about the length property.  Ah well, never mind.

    The method is similiar however.

     

    I answered this at the end of the other thread that you posted in.>>

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2201620&SiteID=1&mode=1

     

    Here it is again.>>

     

    0.15 = 15 percent.

     

     

    Code Block

     

     

    Public Class Form1

     

    'The next highlighted line should be on one line in your code window.>>>>

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    'This could be any number, I chose

    '123456 which is 123457 elements

    'including element zero.

    'Fill the array with some values.

    Dim anArrayOfNumbers(123456) As Double

    For count = 123456 To 0 Step -1

    anArrayOfNumbers(count) = 123456 - count

    Next

    Dim fifteenPerCent As Integer = 0

    ' 1 is added as imagine the array is numbers(99)

    ' then 99 + 1 = 100

    ' 15% of which is 0.15 * 100 = 15

    ' Element numbers(0) needs to be included.>> 

    fifteenPerCent = Int(0.15 * (UBound(anArrayOfNumbers) + 1 ))

     

    Dim total As Double = 0

    'Would do the 1st 15 elements if the array has 100 elements.>>

    For index As Integer = 0 To fifteenPerCent - 1

    total = total + anArrayOfNumbers(index)

    Next

    'One is added as elements zero,one,two and three

    'are four separate elements.

    MessageBox.Show("Average= " & (total / (fifteenPerCent + 1)).ToString)

    End Sub

    End Class

     

     

     

     

    Regards,

     

    John.

     

    Friday, September 28, 2007 2:37 PM