-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_prod.cpp
More file actions
44 lines (32 loc) · 1.28 KB
/
Copy pathsum_prod.cpp
File metadata and controls
44 lines (32 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
By default std::accumulate sums the items in a collection, but that behavior can
be changed by passing it another function as an argument. To calculate the
product of the elements in the collection, pass std::multiplies as the last
argument of std::accumulate.
*/
#include <iostream>
#include <vector>
#include <numeric> // std::accumulate
#include <functional> // std::multiplies
using std::cout;
using std::vector;
double average_scores(const std::vector<double>& scores);
double sum_scores(const std::vector<double>& scores);
double product_scores(const std::vector<double>& scores);
int main()
{
vector<double> scores = {9.520, 4.981, 7.880, 7.763, 5.978, 8.654};
double avg = average_scores(scores);
cout << "average score: " << avg << '\n';
double prod = product_scores(scores);
cout << "product of scores: " << prod << '\n';
}
double average_scores(const std::vector<double>& scores) {
return std::accumulate(scores.cbegin(), scores.cend(), 0) / (double)scores.size();
}
double sum_scores(const std::vector<double>& scores) {
return std::accumulate(scores.cbegin(), scores.cend(), 0);
}
double product_scores(const std::vector<double>& scores) {
return std::accumulate( scores.cbegin(), scores.cend(), 1, std::multiplies<double>() );
}