Average of a stream of numbers | Returns the new average after including x ; Prints average of a stream of numbers ; Driver Code
< ? php function getAvg ( $ x ) { static $ sum ; static $ n ; $ sum += $ x ; return ( ( ( float ) $ sum ) / ++ $ n ) ; } function streamAvg ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ avg = getAvg ( $ arr [ $ i ] ) ; echo " Average ▁ of ▁ " . ( $ i + 1 ) . " ▁ numbers ▁ is ▁ " . $ avg . " ▁ STRNEWLINE " ; } return ; } $ arr = array ( 10 , 20 , 30 , 40 , 50 , 60 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; streamAvg ( $ arr , $ n ) ; ? >