Finding average

Hi, can I know what is the standard the coding for finding average in arduino programming? Like is there any example codes that i can follow?

There's a library.

May i ask where can I find the library for the average function?

Certainly

You don't need a library to find an average. That would be like looking for a library to add two numbers. Here's the stone simplest way (I'll assume you are averaging floats.)

Declare an array to hold the floats, say

float myDatata[50]; //allows up to 50 numbers

Declare a variable to indicate how many elements are actually stored in the array, say

int nmbrFloats = 0;

Declare an integer to indicate where the next data point will go in the array,

int index = 0;

Declare a summing variable,

float myDataSum = 0.0;

Now as your floats arrive (I assume your looking at a data input stream), place them in the array location

myData[index] and increment nmbrFloats

When nmbrFloats becomes large enough to indicate you've received all your data, write a do loop,

for(i = 0; i < nmbrFloats; i++) myDataSum = myDataSum + myData*;*
Finally, your average will then be myDataSum/nmbrFloats.
I hate to carp, but every student I know in eighth grade has been taught how to calculate an average.

jrdoner:
every student I know in eighth grade has been taught how to calculate an average.

However true that may be*, it's irrelevant: I took it that the OP knows the arithmetic, but needed help with the coding. He was asking about a standard (whatever that may mean :slight_smile: ) way of doing things.

And while it's true that you don't need a library (doing it from first principles as you described so well), functions do take the drudge out of stuff.

  • although I'm pretty sure I learned it a good few years before that.