Moving average of array elements
The moving average is a concept used in statistics and signal processing to analyze a sequence of data points by calculating the average of a certain number of adjacent values. It is often used to smooth out fluctuations and reveal underlying trends in data.
Problem Statement
Given an array of integer elements and a specified window size, the problem is to calculate the moving average of the array elements by considering each window of consecutive elements.
Idea to Solve the Problem
The idea to calculate the moving average is straightforward. For each window of consecutive elements, you sum up the elements within that window and then divide by the number of elements in the window to find the average. As you move the window along the array, the calculated average will give you insight into the trend of the data.
Pseudocode
method findAverage(arr, n)
sum = 0.0
for i from 0 to n - 1
sum = sum + arr[i]
if i ≠ 0
print ","
print " " + (sum / (i + 1))
Algorithm Explanation
-
The
findAverage
method takes an arrayarr
and the sizen
of the array as inputs. -
Initialize a variable
sum
to keep track of the sum of elements within the current window. -
Loop through the array from index 0 to
n - 1
. -
For each element, add it to the
sum
. -
If the index
i
is not 0, print a comma to separate the average values. -
Print the calculated moving average, which is the
sum
divided by the current window size(i + 1)
.
Code Solution
// Java program for
// Moving average of array elements
public class Average
{
public void findAverage(int[] arr, int n)
{
double sum = 0.0;
for (int i = 0; i < n; ++i)
{
// Calculate sum
sum += arr[i];
if (i != 0)
{
System.out.print(",");
}
// Display average
System.out.print(" " + (sum / (i + 1)));
}
}
public static void main(String[] args)
{
Average task = new Average();
// Array of integer elements
int[] arr = {
4 , 6 , 9 , 20 , 12 , 34 , 25
};
// Get the size
int n = arr.length;
// Test
task.findAverage(arr, n);
}
}
Output
4.0, 5.0, 6.333333333333333, 9.75, 10.2, 14.166666666666666, 15.714285714285714
// C Program
// Moving average of array elements
#include <stdio.h>
void findAverage(int arr[], int n)
{
double sum = 0.0;
for (int i = 0; i < n; ++i)
{
// Calculate sum
sum += arr[i];
// Display average
printf(" %lf", (sum / (i + 1)));
}
}
int main()
{
// Array of integer elements
int arr[] = {
4 , 6 , 9 , 20 , 12 , 34 , 25
};
// Get the size
int n = sizeof(arr) / sizeof(arr[0]);
// Test
findAverage(arr, n);
return 0;
}
Output
4.000000 5.000000 6.333333 9.750000 10.200000 14.166667 15.714286
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Moving average of array elements
class Average
{
public: void findAverage(int arr[], int n)
{
double sum = 0.0;
for (int i = 0; i < n; ++i)
{
// Calculate sum
sum += arr[i];
if (i != 0)
{
cout << ",";
}
// Display average
cout << " " << (sum / (i + 1));
}
}
};
int main()
{
Average *task = new Average();
// Array of integer elements
int arr[] = {
4 , 6 , 9 , 20 , 12 , 34 , 25
};
// Get the size
int n = sizeof(arr) / sizeof(arr[0]);
// Test
task->findAverage(arr, n);
return 0;
}
Output
4, 5, 6.33333, 9.75, 10.2, 14.1667, 15.7143
// Include namespace system
using System;
// Csharp program for
// Moving average of array elements
public class Average
{
public void findAverage(int[] arr, int n)
{
double sum = 0.0;
for (int i = 0; i < n; ++i)
{
// Calculate sum
sum += arr[i];
if (i != 0)
{
Console.Write(",");
}
// Display average
Console.Write(" " + (sum / (i + 1)));
}
}
public static void Main(String[] args)
{
Average task = new Average();
// Array of integer elements
int[] arr = {
4 , 6 , 9 , 20 , 12 , 34 , 25
};
// Get the size
int n = arr.Length;
// Test
task.findAverage(arr, n);
}
}
Output
4, 5, 6.33333333333333, 9.75, 10.2, 14.1666666666667, 15.7142857142857
package main
import "fmt"
// Go program for
// Moving average of array elements
type Average struct {}
func getAverage() * Average {
var me *Average = &Average {}
return me
}
func(this Average) findAverage(arr[] int, n int) {
var sum float64 = 0.0
for i := 0 ; i < n ; i++ {
// Calculate sum
sum += float64(arr[i])
if i != 0 {
fmt.Print(",")
}
// Display average
fmt.Print(" ", (sum / float64(i + 1)))
}
}
func main() {
var task * Average = getAverage()
// Array of integer elements
var arr = [] int { 4 , 6 , 9 , 20 , 12 , 34 , 25 }
// Get the size
var n int = len(arr)
// Test
task.findAverage(arr, n)
}
Output
4.0, 5.0, 6.333333333333333, 9.75, 10.2, 14.166666666666666, 15.714285714285714
<?php
// Php program for
// Moving average of array elements
class Average
{
public function findAverage($arr, $n)
{
$sum = 0.0;
for ($i = 0; $i < $n; ++$i)
{
// Calculate sum
$sum += $arr[$i];
if ($i != 0)
{
echo(",");
}
// Display average
echo(" ".($sum / ($i + 1)));
}
}
}
function main()
{
$task = new Average();
// Array of integer elements
$arr = array(4, 6, 9, 20, 12, 34, 25);
// Get the size
$n = count($arr);
// Test
$task->findAverage($arr, $n);
}
main();
Output
4, 5, 6.3333333333333, 9.75, 10.2, 14.166666666667, 15.714285714286
// Node JS program for
// Moving average of array elements
class Average
{
findAverage(arr, n)
{
var sum = 0.0;
for (var i = 0; i < n; ++i)
{
// Calculate sum
sum += arr[i];
if (i != 0)
{
process.stdout.write(",");
}
// Display average
process.stdout.write(" " + (sum / (i + 1)));
}
}
}
function main()
{
var task = new Average();
// Array of integer elements
var arr = [4, 6, 9, 20, 12, 34, 25];
// Get the size
var n = arr.length;
// Test
task.findAverage(arr, n);
}
main();
Output
4, 5, 6.333333333333333, 9.75, 10.2, 14.166666666666666, 15.714285714285714
# Python 3 program for
# Moving average of array elements
class Average :
def findAverage(self, arr, n) :
sum = 0.0
i = 0
while (i < n) :
# Calculate sum
sum += arr[i]
if (i != 0) :
print(",", end = "")
# Display average
print(" ", (sum / (i + 1)), end = "")
i += 1
def main() :
task = Average()
# Array of integer elements
arr = [4, 6, 9, 20, 12, 34, 25]
# Get the size
n = len(arr)
# Test
task.findAverage(arr, n)
if __name__ == "__main__": main()
Output
4.0, 5.0, 6.333333333333333, 9.75, 10.2, 14.166666666666666, 15.714285714285714
# Ruby program for
# Moving average of array elements
class Average
def findAverage(arr, n)
sum = 0.0
i = 0
while (i < n)
# Calculate sum
sum += arr[i]
if (i != 0)
print(",")
end
# Display average
print(" ", (sum / (i + 1)))
i += 1
end
end
end
def main()
task = Average.new()
# Array of integer elements
arr = [4, 6, 9, 20, 12, 34, 25]
# Get the size
n = arr.length
# Test
task.findAverage(arr, n)
end
main()
Output
4.0, 5.0, 6.333333333333333, 9.75, 10.2, 14.166666666666666, 15.714285714285714
// Scala program for
// Moving average of array elements
class Average()
{
def findAverage(arr: Array[Int], n: Int): Unit = {
var sum: Double = 0.0;
var i: Int = 0;
while (i < n)
{
// Calculate sum
sum += arr(i);
if (i != 0)
{
print(",");
}
// Display average
print(" " + (sum / (i + 1)));
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Average = new Average();
// Array of integer elements
var arr: Array[Int] = Array(4, 6, 9, 20, 12, 34, 25);
// Get the size
var n: Int = arr.length;
// Test
task.findAverage(arr, n);
}
}
Output
4.0, 5.0, 6.333333333333333, 9.75, 10.2, 14.166666666666666, 15.714285714285714
import Foundation;
// Swift 4 program for
// Moving average of array elements
class Average
{
func findAverage(_ arr: [Int], _ n: Int)
{
var sum: Double = 0.0;
var i: Int = 0;
while (i < n)
{
// Calculate sum
sum += Double(arr[i]);
if (i != 0)
{
print(",", terminator: "");
}
// Display average
print(" ", (sum / Double(i + 1)), terminator: "");
i += 1;
}
}
}
func main()
{
let task: Average = Average();
// Array of integer elements
let arr: [Int] = [4, 6, 9, 20, 12, 34, 25];
// Get the size
let n: Int = arr.count;
// Test
task.findAverage(arr, n);
}
main();
Output
4.0, 5.0, 6.33333333333333, 9.75, 10.2, 14.1666666666667, 15.7142857142857
// Kotlin program for
// Moving average of array elements
class Average
{
fun findAverage(arr: Array < Int > , n: Int): Unit
{
var sum: Double = 0.0;
var i: Int = 0;
while (i < n)
{
// Calculate sum
sum += arr[i];
if (i != 0)
{
print(",");
}
// Display average
print(" " + (sum / (i + 1)));
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Average = Average();
// Array of integer elements
val arr: Array < Int > = arrayOf(4, 6, 9, 20, 12, 34, 25);
// Get the size
val n: Int = arr.count();
// Test
task.findAverage(arr, n);
}
Output
4.0, 5.0, 6.333333333333333, 9.75, 10.2, 14.166666666666666, 15.714285714285714
Time Complexity
The time complexity of this algorithm is O(n), where n is the number of elements in the array. This is because the algorithm iterates through the array once, performing constant-time operations for each element.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment