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