Skip to content

Instantly share code, notes, and snippets.

@saschagrunert
Last active May 13, 2018 13:35
Show Gist options
  • Save saschagrunert/67e0194b7cfbfc8c124f11869f60cdbd to your computer and use it in GitHub Desktop.
Save saschagrunert/67e0194b7cfbfc8c124f11869f60cdbd to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
auto average(const std::vector<uint32_t> & input) -> double;
int main(void) {
// Create a vector
std::vector<uint32_t> v = {9, 8, 3};
// Get the average
const double avg = average(v);
// Results in "Average: 6.6667"
std::cout << "Average: " << avg;
return 0;
}
double average(const std::vector<uint32_t> & input) {
uint32_t sum = 0;
for (uint32_t i = 0; i < input.size(); ++i) {
sum += input[i];
}
return sum / (double)input.size();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment