Friday, March 21, 2014

Array with Method_Ex_3 (Wk 8) Feb 24-28, 2014

Q: Write method to return the average of all the values.

  static void Main(string[] args)
        {
            int[] A = { 12, -5, -9, 3, 6, 19, -13, -22, 33, 1, -17, 29 };

            float result = AverageOfAllValue(A);
            Console.WriteLine("Average of all value is: {0:f2}", result);


            Console.ReadLine();
        }
        static float AverageOfAllValue (int[] A)
        {
            float sum = 0;
            int counter = 0;
            for (int i = 0; i < A.Length; i++)
            {
                sum += A[i];
                counter++;
            }
            float avg = sum / counter;
            return avg;
        }
    }
}

No comments:

Post a Comment