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