Page 1 of 1

[HD Pg 182, Sec. 8.1.3 - varargs]

Posted: Tue Feb 19, 2019 2:20 pm
by OCAJO1

Code: Select all

public double average(int[] values){
    /* by the way, can you tell what will happen if sum is declared as int?
    double sum = 0;
    for(int i=0; i<values.length; i++) sum += values[i];
        return values.length==0?0 : sum/values.length;
}
Can't test this at this time, but I don't see that anything will change if sum is declared as an int.

Re: [HD Pg 182, Sec. 8.1.3 - varargs]

Posted: Tue Feb 19, 2019 8:54 pm
by admin
Yes, nothing much. sum/values.length will perform an integer division instead of double. So, the average value will not be accurate.

Re: [HD Pg 182, Sec. 8.1.3 - varargs]

Posted: Wed Feb 20, 2019 12:37 pm
by OCAJO1
keep missing the obvious :oops: