2021年3月3日星期三

How do I calculate average for 2D array?

I wanted to know whether the calculation for average in 2D arrays is the same as 1D arrays? Can I also see a java code for it? This is what I have but I am not getting the correct answer.

   static int avgRecursion(int a[][], int i, int j,int f)  {        // Last element      if (i == f-1)          return a[i][j];        // When index is 0, divide sum computed so far by n.      if (i == 0)          return ((a[i][j] + avgRecursion(a, i+1,j, f))/f);        // Compute sum      return (a[i][j] + avgRecursion(a, i+1,j, f));  }    static int[][] array;  static {      array = new int[3][4];          System.out.println("Enter 12 numbers: ");      Scanner scan = new Scanner(System.in);        for (int i = 0; i < 3; i++) {          for (int j = 0; j < 4; j++) {              array[i][j] = scan.nextInt();          }      }      int f = array.length;      int average = avgRecursion(array, 0,0, f);      System.out.println("The average of the 2D array: " + average);      }       }  
https://stackoverflow.com/questions/66466899/how-do-i-calculate-average-for-2d-array March 04, 2021 at 08:29AM

没有评论:

发表评论