Reputation: 77
I'm trying to average a 2D array column. My code seems to add the number in the row and then divide by the total. In this example there should only be 4 assignments, yet it loops 12 times. Anyone know what's wrong with my code? The numbers are being inputted from a text file, higher up in the full code (not sure if that affects it).
Example Arrays:
[2,3,6,7]
[4,5,6,7]
[2,2,2,2]
System.out.println("Average score of each assignment:");
//TODO: compute and print the average on each assignment
double total = 0;
int totallength = 0;
int assignment = 1;
for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray[i].length; j++) {
total += myArray[i][j];
totallength++;
System.out.println("Assignment #" + assignment++
+ " Average: " + (total / totallength));
}
}
Here is my output:
Average score of each assignment:
Assignment #1 Average: 2.0
Assignment #2 Average: 2.5
Assignment #3 Average: 3.6666666666666665
Assignment #4 Average: 4.5
Assignment #5 Average: 4.4
Assignment #6 Average: 4.5
Assignment #7 Average: 4.714285714285714
Assignment #8 Average: 5.0
Assignment #9 Average: 4.666666666666667
Assignment #10 Average: 4.4
Assignment #11 Average: 4.181818181818182
Assignment #12 Average: 4.0
Desired Output:
Assignment #1 Average: 2.666666666
Assignment #2 Average: 3.333333333
Assignment #3 Average: 4.666666666
Assignment #4 Average: 5.333333333
Upvotes: 2
Views: 75
Reputation:
You can use IntStream.average()
method:
int m = 3;
int n = 4;
int[][] arr = {
{2, 3, 6, 7},
{4, 5, 6, 7},
{2, 2, 2, 2}};
double[] averages = IntStream.range(0, n)
.mapToDouble(i -> IntStream.range(0, m)
.map(j -> arr[j][i])
.average()
.orElse(0))
.toArray();
// output
IntStream.range(0, averages.length).forEach(i ->
System.out.println("Assignment #" + (i + 1)
+ " Average: " + (averages[i])));
Assignment #1 Average: 2.6666666666666665
Assignment #2 Average: 3.3333333333333335
Assignment #3 Average: 4.666666666666667
Assignment #4 Average: 5.333333333333333
Upvotes: 0
Reputation: 79075
You can do it as follows:
public class Main {
public static void main(String[] args){
int [][]myArray= {
{2,3,6,7},
{4,5,6,7},
{2,2,2,2},
};
double total=0;
int assignment=1;
System.out.println("Average score of each assignment:");
for(int i=0;i<myArray[0].length;i++) {
for(int j=0;j<myArray.length;j++) {
total+=myArray[j][i];
}
System.out.println("Assignment #" + assignment++ + " Average: " + (total/3));
total=0;
}
}
}
Output:
Average score of each assignment:
Assignment #1 Average: 2.6666666666666665
Assignment #2 Average: 3.3333333333333335
Assignment #3 Average: 4.666666666666667
Assignment #4 Average: 5.333333333333333
Upvotes: 1
Reputation: 12953
if you want to get the average of each of the rows, you need to initialize your total counters on each oter loop, and also print only on the outer loop after summing:
int assignment=1;
for(int i=0;i<myArray.length;i++) {
// initialize here, to start each row separatly
double total=0;
int totallength=0;
for(int j=0;j<myArray[i].length;j++) {
total+=myArray[i][j];
totallength++;
}
// print after the loop, so it will be done only once per row
System.out.println("Assignment #" + assignment++ + " Average: " + (total/totallength));
}
BTW, much easier way to calculate the averages will be to utilize java 8 stream capabilities:
for(int i=0;i<myArray.length;i++) {
double average = Arrays.stream(myArray[i]).average().orElse(0d);
}
Upvotes: 0