averaging 
Author Message
 averaging

Hey this might be a dumb question but i really need help
on this, can someone help me write a little program that
takes any amount of numbers and averages them? thanks ALOT


Tue, 26 Oct 2004 23:50:26 GMT  
 averaging


Quote:
> Hey this might be a dumb question but i really need help
> on this, can someone help me write a little program that
> takes any amount of numbers and averages them? thanks ALOT

Think of how you'd do this using pencil and paper......you'd add up all the
numbers and then divide their sum by however many numbers you added.

Consider:

1,5,10,50

Add these up and you get 66.  Divide 66 by 4 and you get 16.5....the average
of those 4 numbers.

Now just write code to do the same thing.  If the numbers are in an array,
it's real easy. It's hard to give you any specific tips because you didn't
provide a lot of information....such as the source of the numbers you want
averaged.

BTW, what does this question have to do with COM, controls, or databases?
Either the vb.syntax or vb.general.discussion newsgroups would have been
much more appropriate.

Mike



Wed, 27 Oct 2004 00:37:35 GMT  
 averaging
On Fri, 10 May 2002 08:50:26 -0700, "bob mcenzie"

Quote:

>Hey this might be a dumb question but i really need help
>on this, can someone help me write a little program that
>takes any amount of numbers and averages them? thanks ALOT

What code have you tried so far?

You will probably want to start off with an array to hold all of the
values.  You can then loop through the array to add the values and
then divide by the number of values after the loop has finished.  If
you run into trouble, post the code you are having problems with and
I'm sure someone will be able to point you in the right direction.

Also, the groups you have posted this to are probably not the best
choice for your question.  You might get better results if you post to
the vb.general.discussion group.

HTH,
Bryan
____________________________________________________________
New Vision Software            "When the going gets weird,"
Bryan Stafford                   "the weird turn pro."

Microsoft MVP-Visual Basic     Fear and Loathing in LasVegas



Wed, 27 Oct 2004 00:39:51 GMT  
 averaging
Your question makes me think you are probably a newby. Excues me if I'm
wrong. Anyway.
If you are trying to average a bunch of numbers that have been placed in
textboxes the best approach is to make the textboxes part of a control
array. That's another discussion. Let's say you have txtAverage(0) through
txtAverage(9), your code might look like:

    Dim i as integer
    Dim nNumber as Single
    dim nAverage as Single

    nNumber = 0
    For i = txtAverage 0 to 9
        nNumber = nNumber + txtAverage(i).text    'if it makes you feel
safer you might say val( txtAverage(i).text)
    Next i
    nAverage = nNumber / 10

    If you are using a control array where you cannot know ahead of time how
many there are, the code might look like
    For i = 0 to txtAverage.Count-1
        nNumber = nNumber + txtAverage(i).text
    Next i
    nAverage = nNumber / (i - 1)

    Then of course you do what ever rounding, etc needs to be done.

  ------------------------------------------
If you are reading numbers from a table, then you apply the same type of
logic.
If you are getting one long string of numbers delimited by, say, a comma,
look up the function Split.


Quote:
> Hey this might be a dumb question but i really need help
> on this, can someone help me write a little program that
> takes any amount of numbers and averages them? thanks ALOT



Wed, 27 Oct 2004 01:31:53 GMT  
 averaging
If your numbers are in an Access Database then SQL will average them for
you:

Select avg(itemsTotal) from orders

Some databases also support MIN,MAX,SUM

--
Michael Collier Bsc (Hons)
Interactive ADO Object Browser
www.adoanywhere.com

--


Quote:
> Hey this might be a dumb question but i really need help
> on this, can someone help me write a little program that
> takes any amount of numbers and averages them? thanks ALOT



Wed, 27 Oct 2004 02:56:37 GMT  
 averaging

Quote:

> Hey this might be a dumb question but i really need help
> on this, can someone help me write a little program that
> takes any amount of numbers and averages them? thanks ALOT

I'll have to assume the usual methods aren't working out.  This solves
a slightly different problem, but you shouldn't have too much trouble
adapting it...

Public Function Deviants(ByRef Array1() As Double, _
  Optional ByVal Tolerance As Double = 0.05) As Long

  Dim LB As Long: LB = LBound(Array1)
  Dim UB As Long: UB = UBound(Array1)
  Dim C As Double: C = UB - LB + 1

  Dim M1 As Long: M1 = CLng(Rnd * (UB - LB)) + LB
  Dim M2 As Long: M2 = CLng(Rnd * (UB - LB)) + LB

  Debug.Assert LB <= M1 And M1 <= UB
  Debug.Assert LB <= M2 And M2 <= UB

  Dim M As Double, T As Double, i As Long
  M = Array1(LB) / 4 + Array1(M1) / 4 + Array1(M2) / 4 + Array1(UB) / 4
  For i = LB To UB
    T = T + (Array1(i) - M) / C
  Next
  M = M + T

  For i = LB To UB
    If Abs((Array1(i) - M) / M) > Tolerance Then Deviants = Deviants + 1
  Next
End Function

Interestingly, if you may clobber the array, it might be faster to find
the median.

--
Joe Foster <mailto:jlfoster%40znet.com>     Got Thetans? <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Wed, 27 Oct 2004 08:39:03 GMT  
 averaging

Quote:

> If your numbers are in an Access Database then SQL will average them for
> you:

> Select avg(itemsTotal) from orders

> Some databases also support MIN,MAX,SUM

What, no PRODUCT?  (Although if you do have ABS, EXP, and LOG... >=)

--
Joe Foster <mailto:jlfoster%40znet.com>     On the cans? <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Wed, 27 Oct 2004 08:42:29 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Calculating Average of Grade Fields

2. Moving Average Problems

3. Finding the average of certain fields in a table

4. Average Time

5. Rolling Averages - back to the drawing board

6. Rolling / moving averages

7. Weigted average Cost

8. Sum of average rates

9. Daily Average for Month

10. Need Help with "Average" function

11. VBA return average round trip time

12. Average of array

 

 
Powered by phpBB® Forum Software