Program for average of an array without running into overflow | C # program for the above approach ; Function to calculate average of an array using standard method ; Stores the sum of array ; Find the sum of the array ; Return the average ; Function to calculate average of an array using efficient method ; Store the average of the array ; Traverse the array arr [ ] ; Update avg ; Return avg ; Driver Code ; Input ; Function call
using System ; class GFG { static double average ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; return ( double ) ( sum / N ) ; } static double mean ( int [ ] arr , int N ) { double avg = 0.0 ; for ( int i = 0 ; i < N ; i ++ ) { avg += ( ( double ) ( ( arr [ i ] - avg ) / ( i + 1 ) ) ) ; } return avg ; } static public void Main ( ) { int [ ] arr = { Int32 . MaxValue , 1 , 2 } ; int N = arr . Length ; Console . WriteLine ( " Average ▁ by ▁ Standard ▁ method : ▁ " + ( average ( arr , N ) ) . ToString ( " F10" ) ) ; Console . WriteLine ( " Average ▁ by ▁ Efficient ▁ method : ▁ " + ( mean ( arr , N ) ) . ToString ( " F10" ) ) ; } }