samedi 9 janvier 2016

My program gives me a weird number when being averaged (2D array,doubles,random numbers)

I am trying to get the average of random doubles from a 2d array in which the user inputs the # of rows and columns. However whenever I try to get the average I get a number with 2 decimal points.

double avg=0;
double sum=0;

for (int row=0; row<matrix.length; row++) {
    for (int col=0; col<matrix[row].length; col++) {
        sum+=matrix[row][col];//adds all of the values in the array
    }
    avg = sum/(r*c);
    System.out.print(String.format("%.1f",avg));
 }




Aucun commentaire:

Enregistrer un commentaire