Subscribe to Arduino Academy and start learning today for FREE!

Median vs. Average, Arduino Calculations

If we want to average a set of numbers, we add them together, then divide by the number in the set.

150 + 200 + 0 = 350 / 3 = 116 Average

I’m using int and dropping the fractional parts. Use float if you need them.

But if we want the Median, which is the center, or middle number, we have to sort and compare each number to the others to get a list from high to low, and grab the center number. In this case, the Median is 150.

I’ve written a sketch that calculates both so you can compare your own numbers, and make it a much larger list:

Become the Maker you were born to be. Try Arduino Academy for FREE!

int a[] = {150,200,0};
int n = 3;

a[] is your set of values

n is the number of values in the set

https://pastebin.com/Ge0pZGS2

More info on Mean, Median & Average – https://www.vocabulary.com/articles/chooseyourwords/mean-median-average/

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Archives

0
Would love your thoughts, please comment.x
()
x