CS 1713 Introduction to Computer Science Daily Problem

Write a method that takes one two-dimentional array of integers as a parameter and returns the average of the elements of the array. If the array is empty, return 0.0.

Solution:

   public double getAverage(int[][] a) {
      double sum = 0.0;
      int count = 0;
      for (int j=0;j<a.length;j++)
         for (int k=0;k<a[j].length;k++) {
            sum += a[j][k];
            count++;
         }
      if (count == 0)
         return 0.0;
      return sum/count;
   }