MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / average

Method average

src/main/java/com/thealgorithms/maths/Average.java:32–41  ·  view source on GitHub ↗

Computes the arithmetic mean of a double array. The average is calculated as the sum of all elements divided by the number of elements: avg = Σ(numbers[i]) / n. @param numbers a non-null, non-empty array of double values @return the arithmetic mean of the given numbers @

(double[] numbers)

Source from the content-addressed store, hash-verified

30 * @throws IllegalArgumentException if {@code numbers} is {@code null} or empty
31 */
32 public static double average(double[] numbers) {
33 if (numbers == null || numbers.length == 0) {
34 throw new IllegalArgumentException("Numbers array cannot be empty or null");
35 }
36 double sum = 0;
37 for (double number : numbers) {
38 sum += number;
39 }
40 return sum / numbers.length;
41 }
42
43 /**
44 * Computes the arithmetic mean of an {@code int} array.

Calls

no outgoing calls