public static double avg(int[] a) {
int total = 0;
for (int x : a) total += x;
return /* missing */;
}Which expression computes the average correctly?
- A
a.length / total
- B
total / (double) a
- Ccheck_circle
(double) total / a.length
- D
total / a.length
Explanation
Casting total to double forces floating-point division; otherwise integer division would truncate.