Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - How to calculate the average value of each column in 2D array?

I am trying to calculate the average value of columns in 2D array, but I cannot figure out the code. The function should return the average value of each column. And I cannot print the result in the function. The print should be in main function.

static double average_columns(double matrix[][]) {
    int i, j, sum = 0, average = 0;
    for (i = 0; i < matrix.length; i++) {
        for (j = 0; j < matrix[i].length; j++) {
            sum = (int) (sum + matrix[i][j]);
        }
        average = sum / matrix[i].length;
        sum = 0;
    }
    return average;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is how you can calculate sum and averages of each row and columns : The below example code will print the sum and averages in the same method and wont return any value. If you need to return the actual sum and average then you need to return double[] which will contains all the sums or averages instead of a double.

CODE

public class Test {
    
    static int m = 3; 
    static int n = 3; 

    public static void main(String[] args) {
        
        int i,j;
        int [][]matrix = new int[m][n]; 
      
        int x = 1; // x fills up the value of the matrix
        for (i = 0; i < m; i++) 
            for (j = 0; j < n; j++) 
                matrix[i][j] = x++; 
      
        
        System.out.println("The matrix is : 
");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                System.out.print(matrix[i][j] + "    ");
            }
            System.out.println();
        }
        
        System.out.println("
Printing the avg of each col ::");
        average_columns(matrix);
        
        System.out.println("
Printing the avg of each row ::");
        average_rows(matrix);
        
        System.out.println("
Printing the sum of each col ::");
        sum_columns(matrix);
        
        System.out.println("
Printing the sum of each row ::");
        sum_rows(matrix);

    }
    
    public static void average_rows(int matrix[][]) {
        int i, j;
        double sum = 0, average = 0;
        for (i = 0; i < matrix.length; i++) {
            for (j = 0; j < matrix[i].length; j++) {
                sum=sum+matrix[i][j];
            }
            average=sum/matrix[i].length;
            System.out.println("Average of row " + (i+1) + " = " + average); 
            sum=0;
        }
    }
    
    public static void average_columns(int matrix[][]) {
        int i, j;
        double sum = 0, average = 0;
        for (i = 0; i < matrix.length; i++) {
            for (j = 0; j < matrix[i].length; j++) {
                sum=sum+matrix[j][i];
            }
            average=sum/matrix[i].length;
            System.out.println("Average of column " + (i+1) + " = " + average);
            sum=0;
        }
    }
    
    public static void sum_columns(int matrix[][]) { 
          
        int i,j;
        double sum = 0;       
        for (i = 0; i < matrix.length; ++i) { 
            for (j = 0; j < matrix.length; ++j) { 
                sum = sum + matrix[j][i]; 
            } 
            System.out.println("Sum of column " + (i+1) + " = " + sum); 
            sum = 0; 
        } 
    } 
    
    public static void sum_rows(int matrix[][]) { 
          
        int i,j;
        double sum = 0;   
        for (i = 0; i < matrix.length; ++i) { 
            for (j = 0; j < matrix.length; ++j) { 
                sum = sum + matrix[i][j]; 
            } 
            System.out.println( "Sum of row " + (i+1) + " = " + sum); 
            sum = 0; 
        } 
    } 


}

OUTPUT

The matrix is : 

1    2    3    
4    5    6    
7    8    9    

Printing the avg of each col ::
Average of column 1 = 4.0
Average of column 2 = 5.0
Average of column 3 = 6.0

Printing the avg of each row ::
Average of row 1 = 2.0
Average of row 2 = 5.0
Average of row 3 = 8.0

Printing the sum of each col ::
Sum of column 1 = 12.0
Sum of column 2 = 15.0
Sum of column 3 = 18.0

Printing the sum of each row ::
Sum of row 1 = 6.0
Sum of row 2 = 15.0
Sum of row 3 = 24.0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...