-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cpp
More file actions
66 lines (54 loc) · 1.48 KB
/
search.cpp
File metadata and controls
66 lines (54 loc) · 1.48 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
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
bool search(vector<int> vector, int n);
vector<int> unique(vector<int> vector);
int main() {
vector<int> test;
for (int i = 0; i < 11; i++) {
test.push_back(i);
test.push_back(i);
}
bool is5here = search(test, 5);
cout << is5here << endl;
test = unique(test);
for (size_t i = 0; i < test.size(); i++) {
cout << test[i] << " ";
}
return 0;
}
/* TODO: write a function that takes a vector and searches for
* a particular value x, returning true if and only if x is found. */
bool search(vector<int> vector, int n) {
//Iterate, if found, true
for (size_t i = 0; i < vector.size(); i++) {
if (vector[i] == n) {
return true;
}
}
return false;
}
/* TODO: write a function that takes a vector V of integers which
* is *sorted*, and produces a vector of the unique integers from V.
* E.g., if V = {1,2,2,3,3,3,4}, then the result should
* be {1,2,3,4}. Ideally, modify the vector *in-place*.
* That is, modify the vector V that is given to the function directly,
* and don't create any new vectors. As a warm up: return a new vector with
* the
* result.
* */
vector<int> unique(vector<int> vector) {
//set first value
//if next value is equal to dif
//swap with last and popback
int dif = vector[0];
for (size_t i = 1; i < vector.size(); i++) {
if (vector[i] == dif) {
vector.push_back(vector[i]);
}
}
return vector;
}