You must have read this article and are looking to implement dynamic block size limits. You should know that it's not just a simple formula though that only affects one piece of code, it's more complicated than that.
I know of a few coins that have done this. Monero, Cryptonote, and ziftrCOIN. The latter is the one I am the most familiar with, so I'll explain how the solution works.
First, the block size and chain size is added to the block index. Then, whenever the tip of the chain is updated, the code checks, once every 3 months, to see if both:
- The average block size is greater than 2/3 of the current limit.
- The median block size is greater than 1/2 of the current limit.
If both are true, then it allows the maximum block size to increase by 10%. If this happened regularly for 2 years, the maximum block size would approximately double.