- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Average of a stream of numbers in C++

**Average** of numbers is the sum of numbers divided by the total number of numbers.

In this problem, we are given a stream of numbers. And we will print average of the number at every point.

Let’s take an example of how it works −

We have a stream of 5 number 24 , 76 , 29, 63 , 88

The average at each point of the stream would be −

24 , 50 , 43 , 48 , 56.

For this we will find the average of the stream every time a number is added to the stream. So, we need to find the average of 1 number , 2 numbers , 3 numbers and so on. We will make use of previous average for this.

## Algorithm

Step 1 : for i -> 0 to n (length of stream). Step 2 : find the average of elements using formula : Average = (average * i) + i / (i+1) Step 3 : print average.

## Example

#include <iostream> using namespace std; int main(){ int arr[] = { 24 , 76 , 29, 63 , 88 }; int average = 0; int n = sizeof(arr) / sizeof(arr[0]); for(int i = 0 ; i< n ; i++){ average = ((average * i) + arr[i]) / (i+1); cout<<"The average of "<<i+1<<" numbers of the stream is "<<average<<endl; } return 0; }

## Output

The average of 1 numbers of the stream is 24 The average of 2 numbers of the stream is 50 The average of 3 numbers of the stream is 43 The average of 4 numbers of the stream is 48 The average of 5 numbers of the stream is 56

The same algorithm is applicable for all data types. And can be used to calculate the average of stream at every point.

- Related Questions & Answers
- Average of max K numbers in a stream in C++
- Average of Squares of Natural Numbers?
- Median Of Running Stream of Numbers in C++
- Average of Squares of n Natural Numbers?
- Calculate average of numbers in a column MySQL query?
- Average of odd numbers till a given odd number?
- Average of even numbers till a given even number?
- Average of first n odd naturals numbers?
- Average of first n even natural numbers?
- Realtime moving average of an array of numbers in JavaScript
- 8086 program to find average of n numbers
- Fast average of two numbers without division in C++
- Golang Program to Calculate the Average of Numbers in a Given List
- C++ Program to Calculate Average of Numbers Using Arrays
- Java program to calculate the average of numbers in Java

Advertisements