Program to find Simple Moving Average


In this article, our aim is to determine the Simple Moving Average of the given values in the array. Let us take a quick glance at what Simple Moving Average means.

The average derived from the data over a certain amount of time (t) is known as the simple moving average. In a typical mean, the Simple Moving Average value changes as a result of changing data, but in this kind of mean, it also varies over time. After obtaining the mean for a given period t, some earlier data is then eliminated. We receive new mean once more while this procedure continues. It uses a moving average because of this. This Simple Moving Average has a lot of financial market applications.

Problem Statement

Implement a program to obtain a Simple Moving Average.

Approach

To calculate the simple moving average, we must confirm that all specified items adhere to the programmer's specifications. Furthermore, we must ensure that the total and average values are initialized with zero, failing which they may utilize erroneous values and the result may not be consistent.

We will designate the average as a float or double type of data as our next safety measure. You should not worry if you are a student, but if you decide to employ this program in the business world, there will be millions of calculations. Without taking the decimal numbers into account, the answer will undoubtedly be given, but with a very different result.

Let's use an illustration to make the subject much clearer for you to understand.

Explanation

Consider an integer array as the input.

Input: {1,2,3,4,5}

Let us take the length as 2

Calculation:

Since the length we took is 2, we consider two consecutive digits at a time.

Consider 1 and 2, adding 1+2 we get 3.

The average obtained here is 3/2 that is 1.5

Consider 2 and 3, adding 2+3 we get 5.

The average obtained here is 5/2 that is 2.5

Consider 3 and 4, adding 3+4 we get 7.

The average obtained here is 7/2 that is 3.5

Consider 4 and 5, adding 4+5 we get 9.

The average obtained here is 9/2 that is 4.5

All the averages are considered as the output.

Algorithm

The algorithm to find Simple Moving Average when an array is given is provided below.

Step 1: set the size of the integer array

Step 2: input the elements of the array.

Step 3: Set sum to 0 and movAvg to 0

Step 4: iterate the two for loops.

Step 5: print the result obtained for movAvg=sum/k.

C program to find the simple moving average for the given array is provided below.

Example

#include<stdio.h>
int main() {
   const int n = 10;  //set the array size
   float a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //input array elements
   int k = 2;    // set the length
   float sum = 0;
   float movAvg = 0;
//iterate the loop and check for the condition
   for (int i = 0; i <= ( n - k); i++) {
     sum = 0;            
     for (int j = i; j < i + k; j++) {
       sum += a[j];        
     }
     movAvg = sum / k;
   printf("Simple Moving Average : %.2f \n",movAvg); //print output 
   }
   return 0;
}

Output

On execution, it will produce the following output:

Simple Moving Average : 1.50 
Simple Moving Average : 2.50 
Simple Moving Average : 3.50 
Simple Moving Average : 4.50 
Simple Moving Average : 5.50 
Simple Moving Average : 6.50 
Simple Moving Average : 7.50 
Simple Moving Average : 8.50 
Simple Moving Average : 9.50

Conclusion

Likewise, we can determine the Simple Moving Average by inputting the values of the array.

The challenge of determining the Simple Moving Average of a given array is resolved in this article. Here C programming codes to find the simple moving average are provided.

Updated on: 23-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements