The Wayback Machine - https://web.archive.org/web/20220103163123/https://www.geeksforgeeks.org/program-to-find-simple-moving-average-set-2/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Program to find simple moving average | Set-2

  • Last Updated : 03 Jan, 2022

Simple Moving Average is the average obtained from the data for some t period of time . In normal mean, its value get changed with the changing data but in this type of mean it also changes with the time interval. We get the mean for some period t and then we remove some previous data. Again we get new mean and this process continues. This is why it is moving average. This has a great application in the financial market. OR this can be simply visualized as follows.

Given an arr[] of size N, containing only positive integers and an integer K. The task is to compute the simple moving average of previous K elements. 

Resolution Days 2022 GeeksforGeeks

Examples:

Input: { 1, 3, 5, 6, 8 }, K = 3

Output: 0.33 1.33 3.00 4.67 6.33
Explanation: New number added is 1.0, SMA = 0.33

New number added is 3.0, SMA = 1.33
New number added is 5.0, SMA = 3.0

New number added is 6.0, SMA = 4.67

New number added is 8.0, SMA = 6.33        



Input: Array[]= {2, 5, 7, 3, 11, 9, 13, 12}, K = 2
Output: 1.0 3.5 6 5 7 10 11 12.5

 

Naive Approach: This uses two nested loops. Outer loop traverses the array from left to right. Inner loop computes the average of K previous elements including itself for each index. Finally, the moving average values are printed. The outer loop starts traversal from index K itself. Instead of storing the result, we can directly display the output to avoid using extra spaces.

Time complexity: O(N*K)
Space complexity: O(1)

Efficient Approach: The efficient approach is discussed in the Set-1 of this problem.

Space Optimized approach: This uses sliding window for better time efficiency and space optimization. A window of size K starts from index K and the moving average is printed for each index thereafter. 

Below is the implementation of the above approach. 

C++




// C++ code to find the simple moving average
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
  
// Function to compute moving average
// of previous K elements
void ComputeMovingAverage(int arr[], int N,
                          int K)
{
    int i;
    float sum = 0;
  
    // Initial sum of K elements.
    for (i = 0; i < K; i++) {
        sum += arr[i];
        cout << setprecision(2) << std::fixed;
        cout << sum / K << " ";
    }
  
    // Compute MA from index K
    float avg;
    for (i = K; i < N; i++) {
        sum -= arr[i - K];
        sum += arr[i];
        avg = sum / K;
        cout << setprecision(2) << std::fixed;
        cout << avg << " ";
    }
}
  
// Driver code
int main()
{
    int arr[] = { 1, 3, 5, 6, 8 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 3;
    ComputeMovingAverage(arr, N, K);
    return 0;
}
Output
0.33 1.33 3.00 4.67 6.33 

Time complexity: O(N)
Space complexity: O(1)




My Personal Notes arrow_drop_up

Start Your Coding Journey Now!