Skip to content

Instantly share code, notes, and snippets.

@aaronang
Last active December 10, 2018 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronang/f6ed29430e91b7495a262052f34a77a5 to your computer and use it in GitHub Desktop.
Save aaronang/f6ed29430e91b7495a262052f34a77a5 to your computer and use it in GitHub Desktop.
bool double_eq(const double a, const double b) {
return std::fabs(a - b) < std::numeric_limits<double>::epsilon();
}
double average(const vector<int> &numbers) {
int sum = std::accumulate(numbers.begin(), numbers.end(), 0);
return sum / numbers.size();
}
int main() {
vector<int> numbers = {0, 0, 0, 0, 0};
assert(double_eq(average(numbers), 0));
vector<int> numbers = {1, 0, 10, 0, 0};
assert(double_eq(average(numbers), 2.2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment