# Average of a stream of numbers

Difficulty Level: Rookie
Given a stream of numbers, print average (or mean) of the stream at every point. For example, let us consider the stream as 10, 20, 30, 40, 50, 60, …

```  Average of 1 numbers is 10.00
Average of 2 numbers is 15.00
Average of 3 numbers is 20.00
Average of 4 numbers is 25.00
Average of 5 numbers is 30.00
Average of 6 numbers is 35.00
..................
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

To print mean of a stream, we need to find out how to find average when a new number is being added to the stream. To do this, all we need is count of numbers seen so far in the stream, previous average and new number. Let n be the count, prev_avg be the previous average and x be the new number being added. The average after including x number can be written as (prev_avg*n + x)/(n+1).

## C++

```#include <stdio.h>

// Returns the new average after including x
float getAvg(float prev_avg, int x, int n)
{
return (prev_avg * n + x) / (n + 1);
}

// Prints average of a stream of numbers
void streamAvg(float arr[], int n)
{
float avg = 0;
for (int i = 0; i < n; i++) {
avg = getAvg(avg, arr[i], i);
printf("Average of %d numbers is %f \n", i + 1, avg);
}
return;
}

// Driver program to test above functions
int main()
{
float arr[] = { 10, 20, 30, 40, 50, 60 };
int n = sizeof(arr) / sizeof(arr[0]);
streamAvg(arr, n);

return 0;
}
```

## Java

```// Java program to find average
// of a stream of numbers
class GFG {

// Returns the new average after including x
static float getAvg(float prev_avg, float x, int n)
{
return (prev_avg * n + x) / (n + 1);
}

// Prints average of a stream of numbers
static void streamAvg(float arr[], int n)
{
float avg = 0;
for (int i = 0; i < n; i++)
{
avg = getAvg(avg, arr[i], i);
System.out.printf("Average of %d numbers is %f \n",
i + 1, avg);
}
return;
}

// Driver program to test above functions
public static void main(String[] args)
{
float arr[] = { 10, 20, 30, 40, 50, 60 };
int n = arr.length;
streamAvg(arr, n);
}
}

// This code is contributed by Smitha Dinesh Semwal

```

## PHP

```
<?php
// PHP program for Average of
// a stream of numbers

// Returns the new average
// after including x
function getAvg(\$prev_avg, \$x, \$n)
{
return (\$prev_avg * \$n + \$x) /
(\$n + 1);
}

// Prints average of a
// stream of numbers
function streamAvg(\$arr, \$n)
{
\$avg = 0;
for (\$i = 0; \$i < \$n; \$i++)
{
\$avg = getAvg(\$avg, \$arr[\$i], \$i);
echo "Average of ",\$i + 1, "numbers is "
,\$avg,"\n";
}
return;
}

// Driver Code
\$arr = array(10, 20, 30, 40, 50, 60);
\$n = sizeof(\$arr);
streamAvg(\$arr, \$n);

// This code is contributed by aj_36
?>

```

Output :

```Average of 1 numbers is 10.000000
Average of 2 numbers is 15.000000
Average of 3 numbers is 20.000000
Average of 4 numbers is 25.000000
Average of 5 numbers is 30.000000
Average of 6 numbers is 35.000000
```

The above function getAvg() can be optimized using following changes. We can avoid the use of prev_avg and number of elements by using static variables (Assuming that only this function is called for average of stream). Following is the oprimnized version.

```#include <stdio.h>

// Returns the new average after including x
float getAvg(int x)
{
static int sum, n;

sum += x;
return (((float)sum) / ++n);
}

// Prints average of a stream of numbers
void streamAvg(float arr[], int n)
{
float avg = 0;
for (int i = 0; i < n; i++) {
avg = getAvg(arr[i]);
printf("Average of %d numbers is %f n", i + 1, avg);
}
return;
}

// Driver program to test above functions
int main()
{
float arr[] = { 10, 20, 30, 40, 50, 60 };
int n = sizeof(arr) / sizeof(arr[0]);
streamAvg(arr, n);

return 0;
}
```

Thanks to Abhijeet Deshpande for suggesting this optimized version.

Related article:
Program for average of an array (Iterative and Recursive)

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Practice Tags :
1.6 Average Difficulty : 1.6/5.0
Based on 31 vote(s)