Moving average of array elements

Here given code implementation process.

``````// 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)
{
// Array of integer elements
int[] arr = {
4 , 6 , 9 , 20 , 12 , 34 , 25
};
// Get the size
int n = arr.length;
// Test
}
}``````

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()
{
// Array of integer elements
int arr[] = {
4 , 6 , 9 , 20 , 12 , 34 , 25
};
// Get the size
int n = sizeof(arr) / sizeof(arr[0]);
// Test
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)
{
// Array of integer elements
int[] arr = {
4 , 6 , 9 , 20 , 12 , 34 , 25
};
// Get the size
int n = arr.Length;
// Test
}
}``````

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
}``````

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()
{
// Array of integer elements
\$arr = array(4, 6, 9, 20, 12, 34, 25);
// Get the size
\$n = count(\$arr);
// Test
}
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()
{
// Array of integer elements
var arr = [4, 6, 9, 20, 12, 34, 25];
// Get the size
var n = arr.length;
// Test
}
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() :
#  Array of integer elements
arr = [4, 6, 9, 20, 12, 34, 25]
#  Get the size
n = len(arr)
#  Test

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()
#  Array of integer elements
arr = [4, 6, 9, 20, 12, 34, 25]
#  Get the size
n = arr.length
#  Test
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
}
}``````

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()
{
// Array of integer elements
let arr: [Int] = [4, 6, 9, 20, 12, 34, 25];
// Get the size
let n: Int = arr.count;
// Test
}
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
{
// 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
}``````

Output

`` 4.0, 5.0, 6.333333333333333, 9.75, 10.2, 14.166666666666666, 15.714285714285714``

Comment

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.