{"id":2196,"date":"2015-02-18T12:07:36","date_gmt":"2015-02-18T11:07:36","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2196"},"modified":"2015-02-18T12:07:36","modified_gmt":"2015-02-18T11:07:36","slug":"rosettacode-simple-moving-average","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2196","title":{"rendered":"rosettacode - Simple moving average"},"content":{"rendered":"<p><a href=\"http:\/\/rosettacode.org\/wiki\/Averages\/Simple_moving_average#C.2B.2B\">http:\/\/rosettacode.org\/wiki\/Averages\/Simple_moving_average#C.2B.2B<\/a><\/p>\n<p>The original rosetta C example shows the usage of variable function arguments. The C++ example shows how to implement a circular buffer. They are confusing and inefficient. My Version shows how to implement a simple moving average using modern c++ techniques.<\/p>\n<pre><code>\r\n#include < iostream >\r\n#include < vector >\r\n\r\nclass SMA {\r\npublic:\r\n    SMA(int period) : values(period), sum(0.0), idx(0), size(0) {\r\n    }\r\n\r\n    void add(double value) {\r\n        const size_t period = values.size();\r\n        \/\/ here is the magic. Update sum and vector in place\r\n        sum = sum - values[idx] + value;\r\n        values[idx] = value;\r\n\r\n        \/\/ this implements the circular buffer\r\n        idx = (idx+1)%period;\r\n        if(size+1 <= period) {\r\n            ++size;\r\n        }\r\n    }\r\n\r\n    double avg() const {\r\n        return sum\/size;\r\n    }\r\n\r\nprivate:\r\n    \/\/ store the last seen values\r\n    std::vector<double> values;\r\n    \/\/ store the sum. so we dont have to recalculate it every time\r\n    double sum;\r\n    \/\/ the next element in the buffer we can override\r\n    int idx;\r\n    \/\/ ho many values was allready inserted. This is usually the same as the given periodb\r\n    size_t size;\r\n};\r\n\r\nint main() {\r\n    SMA foo(3);\r\n    SMA bar(5);\r\n\r\n    std::vector<int> data = { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 };\r\n\r\n    for(auto &val : data) {\r\n        foo.add(val);\r\n        std::cout << \"Added \" << val << \" avg: \" << foo.avg() << std::endl;\r\n    }\r\n    std::cout << std::endl;\r\n\r\n    for(auto &val : data) {\r\n        bar.add(val);\r\n        std::cout << \"Added \" << val << \" avg: \" << bar.avg() << std::endl;\r\n    }\r\n\r\n    return 0;\r\n}\r\n\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/rosettacode.org\/wiki\/Averages\/Simple_moving_average#C.2B.2B The original rosetta C example shows the usage of variable function arguments. The C++ example shows how to implement a circular buffer. They are confusing and inefficient. My Version shows how to implement a simple moving average using modern c++ techniques. #include < iostream > #include < vector > class SMA { public: SMA(int [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14,17,40],"class_list":["post-2196","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-c","tag-cpp","tag-rosettacode"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2196"}],"version-history":[{"count":1,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2196\/revisions"}],"predecessor-version":[{"id":2197,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2196\/revisions\/2197"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2196"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}