Program To Find Simple Moving Average In C++ And Javascript

Share Your Love

Program To Find Simple Moving Average In C++ the way we solve this problem is through the below approach. The average obtained from the data for some t period of time is known as a simple moving average. The value of normal mean changes with changing data, whereas the value of this sort of mean changes with the time interval. We calculate the mean for a certain time interval t and then discard some prior data. We receive a new mean, and the process repeats itself. That’s why it’s called a moving average. This has a lot of potential in the financial world. Alternatively, this can be depicted as follows.

Given an N-dimensional array[] with only positive integers and an integer, K. Calculate the simple moving average of the preceding K items.

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: Two nested loops are used in this. The array is traversed from left to right in the outer loop. For each index, the inner loop computes the average of the previous K elements, including itself. The moving average values are finally printed. The traversal of the outer loop begins at index K. Instead of saving the result, we can simply display it to save space.

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

Below is the implementation of the above approach. 

// 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;
}
<script>
	// JavaScript code for the above approach


	// Function to compute moving average
	// of previous K elements
	function ComputeMovingAverage(arr, N,
		K) {
		let i;
		let sum = 0;

		// Initial sum of K elements.
		for (i = 0; i < K; i++) {
			sum += arr[i];
			document.write((sum / K).toFixed(2) + " ");
		}

		// Compute MA from index K
		for (i = K; i < N; i++) {
			sum -= arr[i - K];
			sum += arr[i];
			avg = sum / K;
			document.write((avg).toFixed(2) + " ");
		}
	}

	// Driver code

	let arr = [1, 3, 5, 6, 8];
	let N = arr.length;
	let K = 3;
	ComputeMovingAverage(arr, N, K);

// This code is contributed by Potta Lokesh
</script>

Output:

0.33 1.33 3.00 4.67 6.33 

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

Share Your Love
Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 291

Newsletter Updates

Enter your email address below to subscribe to our newsletter