Accumulators
A Programming Pattern |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
double[]
int
, double
):
String
:
boolean
:
double lowest, result, total; int n; n = Array.getLength(data); total = 0.0; lowest = Double.POSITIVE_INFINITY; for (int i = 0; i < n; i++) { total += data[i]; if (data[i] < lowest) lowest = data[i]; } result = (total - lowest) / (n - 1);
double maximum; int index, n; n = Array.getLength(data); maximum = Double.NEGATIVE_INFINITY; index = -1; for (int i = 0; i < n; i++) { if (data[i] > maximum) { index = i; maximum = data[i]; } }