Expectation or expected value of an array | Java 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
import java . io . * ; 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 ( String args [ ] ) { float expect , n = 6f ; float a [ ] = { 1f , 2f , 3f , 4f , 5f , 6f } ; expect = calc_Expectation ( a , n ) ; System . out . println ( " Expectation ▁ of ▁ array ▁ E ( X ) ▁ is ▁ : ▁ " + expect ) ; } }