how to get average of data in array?

  • 14 years ago


    I made and array from data from my database, and i need now to calculate average? I am usin VB6

    and I am totaly new in Vb, so I would apreciate any kind of help


  • 14 years ago

    The simplest way I guess would be to use a structure as below:

    Dim lCounter As Long
    Dim dTotal As Double
    Dim dAverage As Double

    dTotal = 0
    For lCounter = 0 To UBound(yourArray)
       dTotal = dTotal + yourArray(lCounter)
    dAverage = dTotal / UBound(yourArray)

  • 14 years ago



    dAverage = dTotal / (UBound(yourArray) + 1)

    in VB, if the array is dimensioned as (0 to 5) then it has 6 elements, last element being yourarray(5)

  • 14 years ago

    hey thank you, but now i have one more question, if i want to select some data from an array (for instace all numbers between 5 and 50) and to calculate average, how ? Is there any other easier way to do it or i am going on wrong side of the road?

    I tryed with if loop, but i got some funy numbers?

    Still beginer :(


  • 14 years ago


    There is no way to do it but with a loop and here is the synt.

    Dim lCounter As Long
    Dim dTotal As Double
    Dim dAverage As Double
    Dim StNum As Integer
    Dim LstNum As Integer

    StNum = 5
    LstNum = 55

    dTotal = 0

    For lCounter = StNum To LstNum

       dTotal = dTotal + yourarray(lCounter)

    dAverage = dTotal / (LstNum - StNum - 1)

  • 14 years ago
    Thanks for catching that one.  I guess this shows that answering when in a bit of a hurry isn't the best... :-)
  • 14 years ago

    Again correction

    dAverage = dTotal / (LstNum - StNum + 1)


    5 to 55 (both inclusive) -> 51 numbers

    so (55 - 5 + 1)  = 51

  • 14 years ago

    Hi, Tx for answers, but i gues I wasn´t clear enought in defining my last question.

    when I sad "average of all numbers betwen 5 and 50" I ment on values of the numbers not recordsets, and it is kind of filtering all numbers with values less than 5 and more than 50, and geting average of those numbers.

    Hope it´s better now :)


  • 14 years ago

    dim totunder5 as long, cntunder5 as long

    dim totover50 as long, cntover50 as long

    dim totary as long ,i as long

    for i = 0 to ubound(yourarray)

       totary = totary+yourarray(i)

       select case yourarry(i)

          case >=5

             totunder=totunder 5 + yourarray(i)

             cntunder5 = cntunder5 + 1

          case <=50

             totover50 = totover50 + yourarray(i)

             cntover50 = cntover50 +1

       end select


    dim avgunder5 as single : avgunder5 = totunder5/cntunder5

    dim avgover50as single: avgover50 = totover50/cntover50

    or something along these lines I think

  • 14 years ago
    hmm if i understand well, you calculated average for numbers with values over 5 separetly and values below 50  separetly ? But the point is to calculate average of all those numbers  with values between the limits
  • 14 years ago
    helo, I created an array from my database, arrays consists numbers from 0 to 1000 and more. I need to calculate average of all numbers with values between 7 and 7.5.
    Does anybody know how can I solve this problem ?
  • 14 years ago

    Hi Dragan
     I hope the follwoing example will give a new Idea for u. and also solve ur problem.

    Private Sub Command1_Click()
    Dim intArr(10) As Double
    Dim intCount As Integer
    Dim intUbound As Integer
    Dim dblValue As Double
    incount = 0
    dblValue = 0
    intArr(0) = 10
    intArr(1) = 7
    intArr(2) = 5
    intArr(3) = 9
    intArr(4) = 6
    intArr(5) = 7.5
    intArr(6) = 7
    intArr(7) = 5
    intArr(8) = 4
    intArr(9) = 1

    For intUbound = 0 To UBound(intArr)
        If (intArr(intUbound) >= 7 And intArr(intUbound) <= 7.5) Then
            intCount = intCount + 1
            dblValue = dblValue + intArr(intUbound)
        End If
    Next intUbound
    MsgBox "Total count is : " & CStr(intCount)
    MsgBox "Average Value is : " & CStr(dblValue / intCount)
    End Sub

    Good Luck

    Hari K......

  • 14 years ago

    Dim i%,total&,avg#,icount%
    for i=lbound(array) to ubound(array)
       if array(i)>=7 and array(i)<=75 then
      end if

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”