How to Get the Average of a 2D Array

How to find average of elements in 2d array JAVA?

try this:

Code:

public class AverageElements {
private static double[][] array;

public static void main (String[] args){

// Initialize array
initializeArray();

// Calculate average
System.out.println(getAverage());
}

private static void initializeArray(){
array = new double[5][2];
array[0][0]=1.1;
array[0][1]=12.3;
array[1][0]=3.4;
array[1][1]=5.8;
array[2][0]=9.8;
array[2][1]=5.7;
array[3][0]=4.6;
array[3][1]=7.45698;
array[4][0]=1.22;
array[4][1]=3.1478;
}

private static double getAverage(){
int counter=0;
double sum = 0;
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
sum = sum+array[i][j];
counter++;
}
}

return sum / counter;
}
}

Output:

5.452478000000001

Finding average for a 2d array in python

If you are making a separate function for average

def avg(lst):
lst_el_avg = []
for i in range(len(lst)):
lst_el_avg.append(sum(lst[i])/len(lst))
return sum(lst_el_avg)/len(lst)

Then reference it in your code as follows

print("\n\n" + "The average is:  " + avg(a))

How to find a total average on a multidimensional array (java)

I would try nested for loops. Something like this..

int sum = 0;
int indexCounter = 0; // Use this to keep track of the number of 'entries' in the arrays since they may not be perfectly rectangular.
for(int column = 0; column < temps.length; column++) {
for(int row = 0; row < temps[column].length; row++) {
sum+= temps[column][row];
indexCounter++;
}
}
int average = sum / indexCounter;
System.out.println("The average is " + average);

If you knew for sure that each row had the same number of columns then you could do something like

 int average = sum / (temps.length * temps[0].length)

Trying to get the average for each element in a two dimensional array

I would use List for this

public void studentAverage() throws FileNotFoundException {
File infile= new File("qdata.txt");
Scanner sc = new Scanner(infile);
double rowNum = 0;
List<List<Integer>> grades = new ArrayList<>();
double totalCount = 0.0;
while (sc.hasNext()) {
List<Integer> row = new ArrayList<>();
Scanner lineScanner= new Scanner(sc.nextLine());
while (lineScanner.hasNextInt()) {
row.add(lineScanner.nextInt());
}
grades.add(row);
totalCount += row.size();
}

int index = 0;
for (List<Integer> list : grades) {
for (Integer grade : list) {
System.out.println("Student average is " + (double)Math.round(grade.doubleValue() / totalCount * 10) / 10);
}
}
}

How to find the average of every column in a 2D array?

You're currently adding each element in the row to the variable sum.

You have to change your inner loop to iterate over the number of rows rather than the columns.

public static void columnAverage(int array[][]) {
for (int col = 0; col < array[col].length; col++) {
int sum = 0;
for (int row = 0; row < array.length; row++) {
sum += array[row][col];

}
System.out.println(sum / array.length);
System.out.println();
}
}

2D array column average java

For each test you have to iterate over the students:

for (int test = 0; test < grades.length; test++) {
int total = 0;
for(int student=0; student<names.length; student++) {
total += grades[student][test]
}
double avgForTest = (double) total/names.length;
}


Related Topics



Leave a reply



Submit