# Expectation or expected value of an array

Expectation or expected value of any group of numbers in probability is the long-run average value of repetitions of the experiment it represents. For example, the expected value in rolling a six-sided die is 3.5, because the average of all the numbers that come up in an extremely large number of rolls is close to 3.5. Less roughly, the law of large numbers states that the arithmetic mean of the values almost surely converges to the expected value as the number of repetitions approaches infinity. The expected value is also known as the expectation, mathematical expectation, EV, or first moment.
Given an array, the task is to calculate the expected value of the array.

Examples :

```Input : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
Output : 3.5

Input :[1.0, 9.0, 6.0, 7.0, 8.0, 12.0]
Output :7.16
```

Below is the implementation :

## C++

 `// CPP code to calculate expected  ` `// value of an array ` `#include ` `using` `namespace` `std; ` ` `  `// Function to calculate expectation ` `float` `calc_Expectation(``float` `a[], ``float` `n) ` `{ ` `    ``/*variable prb is for probability  ` `    ``of each element which is same for ` `    ``each element  */` `    ``float` `prb = (1 / n); ` `     `  `    ``// calculating expectation overall ` `    ``float` `sum = 0; ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `        ``sum += a[i] * prb;     ` ` `  `    ``// returning expectation as sum ` `    ``return` `sum; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``float` `expect, n = 6.0; ` `    ``float` `a = { 1.0, 2.0, 3.0,  ` `                 ``4.0, 5.0, 6.0 }; ` `     `  `    ``// Function for calculating expectation ` `    ``expect = calc_Expectation(a, n); ` `     `  `    ``// Display expectation of given array ` `    ``cout << ``"Expectation of array E(X) is : "`  `         ``<< expect << ````" "````; ` `    ``return` `0; ` `} `

## Java

 `// Java code to calculate expected ` `// value of an array ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `    ``// Function to calculate expectation ` `    ``static` `float` `calc_Expectation(``float` `a[], ``float` `n) ` `    ``{ ` `        ``// Variable prb is for probability of each  ` `        ``// element which is same for each element  ` `        ``float` `prb = (``1` `/ n); ` `     `  `        ``// calculating expectation overall ` `        ``float` `sum = ``0``; ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `            ``sum += a[i] * prb;  ` ` `  `        ``// returning expectation as sum ` `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver program ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``float` `expect, n = 6f; ` `        ``float` `a[] = { 1f, 2f, 3f,  ` `                       ``4f, 5f, 6f }; ` `         `  `        ``// Function for calculating expectation ` `        ``expect = calc_Expectation(a, n); ` `         `  `        ``// Display expectation of given array ` `        ``System.out.println(``"Expectation of array E(X) is : "` `                           ``+ expect); ` `    ``} ` `} ` ` `  `// This code is contributed by Anshika Goyal. `

/div>

## Python3

 `# python code to calculate expected  ` `# value of an array ` ` `  `# Function to calculate expectation ` `def` `calc_Expectation(a, n): ` `     `  `    ``# variable prb is for probability  ` `    ``# of each element which is same for ` `    ``# each element  ` `    ``prb ``=` `1` `/` `n ` `     `  `    ``# calculating expectation overall ` `    ``sum` `=` `0` `    ``for` `i ``in` `range``(``0``, n): ` `        ``sum` `+``=` `(a[i] ``*` `prb)  ` `         `  `    ``# returning expectation as sum ` `    ``return` `float``(``sum``) ` ` `  ` `  `# Driver program ` `n ``=` `6``; ` `a ``=` `[ ``1.0``, ``2.0``, ``3.0``,``4.0``, ``5.0``, ``6.0` `] ` ` `  `# Function for calculating expectation ` `expect ``=` `calc_Expectation(a, n) ` ` `  `# Display expectation of given array ` `print``( ``"Expectation of array E(X) is : "``, ` `                                 ``expect ) ` ` `  `# This code is contributed by Sam007 `

## C#

 `// C# code to calculate expected ` `// value of an array ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to calculate expectation ` `    ``static` `float` `calc_Expectation(``float` `[]a, ` `                                    ``float` `n) ` `    ``{ ` `         `  `        ``// Variable prb is for probability ` `        ``// of each element which is same ` `        ``// for each element  ` `        ``float` `prb = (1 / n); ` `     `  `        ``// calculating expectation overall ` `        ``float` `sum = 0; ` `         `  `        ``for` `(``int` `i = 0; i < n; i++)  ` `            ``sum += a[i] * prb;  ` ` `  `        ``// returning expectation as sum ` `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``float` `expect, n = 6f; ` `        ``float` `[]a = { 1f, 2f, 3f,  ` `                    ``4f, 5f, 6f }; ` `         `  `        ``// Function for calculating ` `        ``// expectation ` `        ``expect = calc_Expectation(a, n); ` `         `  `        ``// Display expectation of given ` `        ``// array ` `        ``Console.WriteLine(``"Expectation"` `               ``+ ``" of array E(X) is : "` `                             ``+ expect); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output :

```Expectation of array E(X) is : 3.5
```

Time complexity of the program is O(n).

As we can see that the expected value is actually average of numbers, we can also simply compute average of array.

Randomized