ASMP
ASMP

Reputation: 1

How to call a method that accepts arrays as its parameter on a 2D array?

public static double findAverage(int[]nums)
{
    double total=0;
    double sum =0;
    for(int i=0;i<nums.length;i++)
    {
        total+=nums[i];
    }
    sum+=total/nums.length;
    return sum;
}

I wrote this code to read the array and return the average of all the numbers in it. I need to write another method which uses the method shown above on a 2D array to display a the averages of each row? I have to return an array,.

Upvotes: 0

Views: 83

Answers (1)

Zenkou
Zenkou

Reputation: 31

Here's a simple example that iterates your 2D array and prints the average using findAverage for each row:

public void printAverage(int[][] array) {
  for( int i = 0; i < array.length; ++i ) {
    final int[] row = array[i];
    System.out.println( "Row " + (i + 1) + " average: " + findAverage( row ) );
}

Upvotes: 3

Related Questions