-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.cpp
More file actions
executable file
·57 lines (48 loc) · 1.3 KB
/
Copy pathsol.cpp
File metadata and controls
executable file
·57 lines (48 loc) · 1.3 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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "vectorfunctions.h"
#include <algorithm>
#include <numeric>
using namespace std;
// Reverse a vector.
// Note that it is sent as a reference, so you should
// reverse the same vector that was sent in.
void backwards(vector<int> &vec)
{
reverse(vec.begin(), vec.end());
}
// Return every other element of the vector, starting with the first.
// You should return a new vector with the answer.
// You are not allowed to modify the vector, even though it is
// sent as a reference. Therefore, the parameter is declared "const".
vector<int> everyOther(const vector<int> &vec)
{
vector<int> other;
for (int i = 0; i < vec.size(); ++i)
{
if (i % 2 == 0)
other.push_back(vec[i]);
}
return other;
}
// Return the smallest value of a vector.
int smallest(const vector<int> &vec)
{
return *min_element(vec.begin(), vec.end());
}
// Return the sum of the elements in the vector.
int sum(const vector<int> &vec)
{
return accumulate(vec.begin(), vec.end(), 0);
}
// Return the number of odd integers, that are also on an
// odd index (with the first index being 0).
int veryOdd(const vector<int> &vec)
{
int count = 0;
for (int i = 0; i < vec.size(); ++i)
{
if (i % 2 == 1)
if (vec[i] % 2 == 1)
++count;
}
return count;
}