Friday, January 8, 2010

Need help on a java programming assignment?

I'm uncertain how to start writing this method in the assignment given here:





Write a method that finds the average of all elements in a matrix,


containing 2-dimensional array, of integers. The method should accept matrices of all sizes. (3x3 , 3x4, 4x3, etc.)





Any tips or suggestions would be awesome! thanks!Need help on a java programming assignment?
int matrixAverage(int[][] matrix) {


int count = 0;


int total = 0;


for(int i=0,n=matrix.length; i%26lt;n; i++) {


for(int j=0,k=matrix[i].length; j%26lt;k; j++) {


total += matrix[i][j];


count++;


}


}


int avg = total / count;


return avg;


}





// that is for integers - replace int by float/double if the matrix has other element types

4 comments: