Don't understand what this subroutine is doing? Please Help?-VBForums
Results 1 to 4 of 4

Thread: Don't understand what this subroutine is doing? Please Help?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Don't understand what this subroutine is doing? Please Help?

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    Dim d As Integer
    Dim price(20) As Double ' Adjusted close price
    Dim smm(20) As Double ' Simple moving median

    For i As Integer = LBound(price) To UBound(price)
    price(i) = CInt(Rnd() * 50)
    Next

    d = CInt(txtDay.Text)

    SimpleMovingMedian(price, smm, d) ' calculate the SMM in each daily record
    DisplayRecords(price, smm) ' display the array of DailyRecord in a message box
    End Sub

    ________________________________________________________________


    Public Sub SimpleMovingMedian(ByRef pri() As Double, ByRef sim() As Double, ByVal day As Integer)
    ' calculate the SMM for each daily record
    ' call MEDIAN function to calculate the median of an array

    For i As Integer = LBound(pri) To UBound(pri)
    sim(i) = 0
    Next

    For i As Integer = LBound(pri) To UBound(pri) - day + 1
    sim(i) = Median(pri, i, i + day - 1)
    Next
    End Sub

    What is the for ... next loops in the second subroutine doing? What is the sim() array meaning?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    86,138

    Re: Don't understand what this subroutine is doing? Please Help?

    The second array, i.e. 'sim' would appear to contain the SMM for the first array, 'pri' around the value at the same index. The first loop resets each value in the array to zero and the second loop calculates the SMM around an element in the first array and places it at the same index in the second array.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Re: Don't understand what this subroutine is doing? Please Help?

    Thx a lot. Can I also ask why is there a - day + 1 in the second loop?

    Do you know how I can write a code to sort the elements in an array using a loop?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    86,138

    Re: Don't understand what this subroutine is doing? Please Help?

    The second and third arguments to Median specify the part of the array to use to calculate the median. I don't really understand how you can want to use this code without knowing what it's doing. Do you want to calculate the simple moving median or not, because that's what the code is doing. If you do then surely you must know what a simple moving median is and, therefore, how it's calculated. If you don't then what are you doing with this code in the first place? Also, have you actually stepped through the code and watched it in action?

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.