-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex.cpp
91 lines (61 loc) · 2.09 KB
/
ex.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
void print_vec(std::vector<int> &vec) {
for (auto &el : vec) {
std::cout << el << " ";
}
std::cout << std::endl << std::endl;
}
int main(int argc, char* argv[]) {
// First, a warmup of basic algorithm usage
auto nums = std::vector<int>(50);
// let's initalize our vector with some random numbers
for (int i = 0; i < 50; ++i) {
nums[i] = std::rand() % 100;
}
// Bonus: can we do this using the algorithms library? Hint - std::generate
// and use the following lambda
auto gen = []() { return std::rand() % 100; };
// Your code here....
// Now, sort nums.
// Your code here....
std::cout << "Sorted nums: ";
print_vec(nums);
// Reverse sort nums, using (a) sort on its own and (b) using sort and another
// algorithm function
// Your code here....
std::cout << "Reverse sorted nums (a): ";
print_vec(nums);
// Your code here....
std::cout << "Reverse sorted nums (b): ";
print_vec(nums);
// Now, lets look at a more involved example. We'll be working through Project
// Euler No.2 (https://projecteuler.net/problem=2) "By considering the terms in
// the Fibonacci sequence whose values do not exceed four million, find the sum
// of the even-valued terms"
// First lets get the fist 47 fibonacci numbers
// BONUS: use std::transform
auto fibs = std::vector<int>(47);
// Your code here....
print_vec(fibs);
// Next, get all that are less than or equal to 4 million, and store them in
// fibs_less HINT: use std::copy_if and std::back_inserter
auto fibs_less = std::vector<int>();
// Your code here....
std::cout << "fibs <= 4000000: ";
print_vec(fibs_less);
// Now, get the evens. Use the same approach as above
auto evens = std::vector<int>();
// Your code here....
std::cout << "Evens: ";
print_vec(evens);
// Finally, let's sum them (hint: std::accumulate)
int sum = 0;
std::cout << "Sum of even fibonacci numbers not greater than 4 million: "
<< sum << std::endl;
return 0;
}