Reputation:
this always outputs 1 and i dont know why, im trying to make a method that takes the average of the elements of a 2d array. this is part of a class and im calling it from a main class.
public static double Ave(Array_two a) {
int average = 0;
int total = a.rows * a.cols;
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < cols; j++) {
average = a.values[i][j] / total;
}
}
return average;
Upvotes: 2
Views: 74
Reputation: 27119
There are multiple problems with your code.
First, I guess j < cols
in the inner loop should be j < a.cols
.
Second, and most important, this is not how you calculate the average of a set of numbers.
You should first sum them, then divide the total by the number of samples. Like this, for example
public static double Ave(Array_two a) {
double sum = 0; // you want to return a double
double total = a.rows * a.cols;
if (total == 0) return 0; // watch out for possible division by 0
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
sum += a.values[i][j];
}
}
return sum / total;
}
Upvotes: 1
Reputation: 2417
Double average = Arrays
.stream(new double[][]{{1.2, 3.4}, {1.2, 3.4}})
.mapToDouble(
row -> Arrays.stream(row).average().getAsDouble()
)
.average()
.getAsDouble();
If use java 8
Upvotes: 1
Reputation: 921
As @Federicoklez already commented here are the problem with your code..
1) Instead of j < cols there should be j < a.cols
2) Integer divided by integer yiels an integer i.e 5/2=2 not 2.5
3) For example ,If you want to calculate average of {1,5,2,3} it is calculated as {1+5+2+3}/4 ,i.e. sum of numbers dived by number of items but what you calculated is 1/4+5/4+2/4+3/4;
Now implementing all this in your program will give something like this.
public static double Ave(Array_two a) {
int total = a.rows * a.cols;
double sum=0;
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < cols; j++) {
sum +=a.values[i][j];
}
}
return sum/total;
}
Upvotes: 0
Reputation: 1106
As Federico klez Culloca says in the comments, this isn't quite the correct code to calculate an average. You need to sum all the values in your for loops and then divide by the total number of values afterwards. He is also correct about the fact that the average need to be a double (a decimal) otherwise the result will be an int (a whole number). You should try something like:
public static double Ave(Array_two a) {
double average = 0;
int sum = 0;
int total = a.rows * a.cols;
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
sum += a.values[i][j];
}
}
average = sum / total;
return average;
}
Where sum += a.values[i][j];
means sum = sum + a.values[i][j];
Hope this helps! Feel free to comment if you need more clarification :)
Upvotes: 0
Reputation: 5748
You can get average like this:
public static double Ave(Array_two a) {
int count = a.rows * a.cols;
if (count == 0) {
return 0;
}
double total = 0;
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
total += a.values[i][j];
}
}
return total / count;
}
Upvotes: 1