Results 1 to 6 of 6

Thread: [RESOLVED] coding median in vb6

  1. #1
    Addicted Member
    Join Date
    Oct 11
    Posts
    200

    Resolved [RESOLVED] coding median in vb6

    hi
    i have to show the mid range values on a screen

    so have three text boxes with number in them
    eg
    value1 2
    value1 5
    value1 7
    and want to display the mid range value in a new text box any ideas how to do this

  2. #2
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    Re: coding median in vb6

    i am not sure .what you want exactly .but you can try mid function .
    Last edited by firoz.raj; Aug 11th, 2012 at 03:51 PM. Reason: Modify

  3. #3
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,255

    Re: coding median in vb6

    Also i'm not sure what exactly you want,

    for mid range use the following formula, Mid range = sum of values / number of values

    Code:
    TextBox4.Text = (TextBox1.Text + TextBox2.Text +TextBox3.Text) / 3

  4. #4
    Frenzied Member
    Join Date
    Jan 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    Re: coding median in vb6

    try the following .because if all the textboxes data is a numeric value .
    Code:
    TextBox4.Text = (val(TextBox1.Text) + val(TextBox2.Text) +val(TextBox3.Text)) / 3
    
    or 
    TextBox4.Text = (val(TextBox1) + val(TextBox2) +val(TextBox3)) / 3

  5. #5
    Addicted Member
    Join Date
    Oct 11
    Posts
    200

    Re: coding median in vb6

    hi
    i found some code to help me with this
    Code:
    Const MOODYS_TITLE As String = "Moody's"
    Const SP_TITLE As String = "S&P"
    Const FITCH_TITLE As String = "Fitch"
    
    Dim sRatingAgency As String
    Dim iMidLevelPostNotched As Integer
    
    'Rating Agency
    
    Private Sub Form_Load()
    
    
    'Call Median
    
    
    
            'MsgBox MidLevelPostNotched(17, 0, 10)
          '    MsgBox MidLevelPostNotched(7, 2, 0)
    
                    
                    
                   'median
                    Call MidLevelPostNotched(16, 12, 10) '12
    
              '   MsgBox MidLevelPostNotched(16, 16, 10) ' 16
            'MsgBox MidLevelPostNotched(7, 7, 6)
            'MsgBox MidLevelPostNotched(10, 8, 9)
    
    
        sRatingAgency = ""
        iMidLevelPostNotched = 0
     '   Call MidLevelPostNotched(0, 0, 0)
       ' Call MidLevelPostNotched(10, 16, 0)
    
    '   Call MidLevelPostNotched(16, 16, 10) ' 16
      '     Call MidLevelPostNotched(7, 7, 6)
     '     Call MidLevelPostNotched(10, 8, 9)
            
            
        MsgBox (CStr(iMidLevelPostNotched) + " : " + sRatingAgency)
        
    End
    
    End Sub
    Code:
    Public Sub MidLevelPostNotched(iMoodys As Integer, iSP As Integer, iFitch As Integer)
    
        Dim iHighest As Integer
        Dim iArr As Variant
    
        If iMoodys = 0 Or iSP = 0 Or iFitch = 0 Then
            'get highest value
            iHighest = iMoodys
            sRatingAgency = MOODYS_TITLE
    
            If iSP > iHighest Then
                iHighest = iSP
                sRatingAgency = SP_TITLE
            End If
            If iFitch > iHighest Then
                iHighest = iFitch
                sRatingAgency = FITCH_TITLE
            End If
           iMidLevelPostNotched = iHighest
        Else
            'get median value - Sort the array and get the mid value
            iArr = Array(0, iMoodys, iSP, iFitch)
            ' Sort the Array and display the values in order.
            BubbleSort iArr
            iMidLevelPostNotched = iArr(2)
            
            If iMidLevelPostNotched = iMoodys Then
                sRatingAgency = MOODYS_TITLE
            ElseIf iMidLevelPostNotched = iSP Then
                sRatingAgency = SP_TITLE
            ElseIf iMidLevelPostNotched = iFitch Then
                sRatingAgency = FITCH_TITLE
            End If
            
            
            
        End If
    
    
    End Sub
    Code:
      Function BubbleSort(TempArray As Variant)
              Dim Temp As Variant
              Dim i As Integer
              Dim NoExchanges As Integer
    
              ' Loop until no more "exchanges" are made.
              Do
                  NoExchanges = True
                  ' Loop through each element in the array.
                  For i = 1 To UBound(TempArray) - 1
    
                      ' If the element is greater than the element
                      ' following it, exchange the two elements.
                      If TempArray(i) > TempArray(i + 1) Then
                          NoExchanges = False
                          Temp = TempArray(i)
                          TempArray(i) = TempArray(i + 1)
                          TempArray(i + 1) = Temp
                      End If
                  Next i
              Loop While Not (NoExchanges)
    
          End Function
    it works which is great.
    my problem is i need to change it around a bi. instead of this line
    Call MidLevelPostNotched(16, 12, 10) '12

    i will be reading the number from a text file and looping through it till it finishes.
    now i no i will need an arrary to do this so i can pass the numbers into the MidLevelPostNotched function. but i find it hard to figure out arrays and how do to this
    any one any ideas
    i no it will go something like this
    Code:
    Do While Not EOF(fn)
     Line Input #fn, sBuffer
          '    Input #fn, sBuffer
    
          
    
         
    
                 
    
          
         
    
        Loop
    i can just never get the what i need in the middle part with arrays

  6. #6
    Addicted Member
    Join Date
    Oct 11
    Posts
    200

    Re: coding median in vb6

    Code:
        Do While Not EOF(fn)
     Line Input #fn, sBuffer
          '    Input #fn, sBuffer
    
    
    
    TempArray = Split(sBuffer, ",") ' this will split the data on each comma
    ' convert your text data into integers for the subroutine
    MidLevelPostNotched (TempArray(0)), (TempArray(1)), (TempArray(2))
                 
    
          
         
    
        Loop

Posting Permissions

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