Kirill Konovalov Kirill Konovalov - 1 month ago 8
Java Question

count an average of multidimensional array

I was asked to count an average of a multidimensional array (table). I think that I build everything correctly (at least I feel so):

import java.util.Scanner;

public class Exam {
static int NROWS = 5;
static int NCOL = 6;
static int[][] data = new int[NROWS][NCOL];

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
enterData(scan);
countAvg();
}

static void enterData(Scanner in) {
System.out.println("How many rows ? (up to " + NROWS + ")");
int rows = in.nextInt();
System.out.println("How many cols ? (up to " + NCOL + ")");
int cols = in.nextInt();
for (int i = 0; i < rows; i++) {
System.out.println("Row" + i + ": ");
for (int j = 0; j < cols; j++) {
System.out.print("Enter Data for Column " + j + ":");
data[i][j] = in.nextInt();
}
}
}

static void countAvg() {
int total = 0;
int count = 0;
for (int i = 0; i < NROWS; i++) {
for (int j = 0; j < NCOL; j++) {
total = total + data[i][j];
count = count++;
}
}
int avg = total / count;
System.out.println("the average is: " + avg);
}


}

but the system still pushes me this message:


Exception in thread "main" java.lang.ArithmeticException: / by zero
at Exam.countAvg(Exam.java:33)
at Exam.main(Exam.java:9)


Please help me to understand what am I doing wrong. Many thanks!

Answer

First, change count = count++; to just count++;

Second, you are doing integer division, so change int avg = total/count; to double int = total / (double) count;

Third, you are setting your data array as 5X6, even if they input something different, this will throw off the average as count will add for every cell, 30 in all.

Instead, try this

import java.util.Scanner;

public class Exam {
    static int[][] data;
    private static int rows = 0;
    private static int cols = 0;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        enterData(scan);
        countAvg();
    }

    static void enterData(Scanner in) {
        System.out.println("How many rows ?");
        rows = in.nextInt();
        System.out.println("How many cols ?");
        cols = in.nextInt();
        data = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            System.out.println("Row" + i + ": ");
            for (int j = 0; j < cols; j++) {
                System.out.print("Enter Data for Column " + j + ":");
                data[i][j] = in.nextInt();
            }
        }
    }

    static void countAvg() {
        int total = 0;
        int count = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                total = total + data[i][j];
                count++;
            }
        }
        double avg = total / (double) count;
        System.out.println(count + "the average is: " + avg);
    }
}

This will allow you to create a new data array with the input rows and cols, which allows you to calculate the average correctly.