-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectors.cpp
More file actions
122 lines (106 loc) · 2.63 KB
/
vectors.cpp
File metadata and controls
122 lines (106 loc) · 2.63 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <vector>
using std::vector;
void printVector(const vector<int> V);
void reverse(vector<int>& V);
vector<int> uniquify(const vector<int>& V);
void push_front(vector<int>& V, int input);
bool exprSum(const vector<int> V, int input);
bool search(const vector<int> V, int input);
int main() {
vector<int> vector = {3,3,3,2,2,1};
push_front(vector, 4);
push_front(vector, 5);
cout << "The original vector is: " << endl;
printVector(vector);
cout << endl;
cout << "Reversing the vector: " << endl;
reverse(vector);
printVector(vector);
cout << endl;
cout << "Unqifying the vector: " << endl;
printVector(uniquify(vector));
cout << endl;
cout << "Are there two elements that add up to 6?" << endl;
cout << exprSum(vector, 6);
cout << endl;
cout << "Is there an element that is 4?" << endl;
cout << exprSum(vector, 4);
cout << endl;
return 0;
}
void printVector(const vector<int> V) {
//Prints the given vector
for (size_t i = 0; i < V.size(); i++) {
cout << V[i] << " ";
}
}
void reverse(vector<int>& V) {
//Reverses the given vector (CLASS)
size_t n = V.size();
for (size_t i = 0; i < n/2; i++) {
//Swap V[i] with V[n-i-1]
V[i] ^= V[n-i-1];
V[n-i-1] ^= V[i];
V[i] ^= V[n-i-1];
// ^= is xor
}
}
vector<int> uniquify(const vector<int>& V) {
//Returns unique values of vector (CLASS)
vector<int> U;
//If empty, return itself
if (V.size() == 0)
return U;
//Set first value
U.push_back(V[0]);
//Iterate and scan
for (size_t i = 1; i < V.size(); i++) {
//If V is different from last, put it in U
if (V[i] != U[U.size()-1])
U.push_back(V[i]);
}
return U;
}
void push_front(vector<int>& V, int input) {
//Pushes a value into the front of a vector
V.push_back(input);
size_t n = V.size();
//Swap with the last element
for (size_t i = 0; i < n; i++) {
int temp = V[i];
V[i] = V[V.size() - 1];
V[V.size() - 1] = temp;
}
}
bool exprSum(const vector<int> V, int input) {
//Can given value be expressed as a sum of two elements
for (size_t i = 0; i < V.size(); i++) {
for (size_t j = i; j < V.size(); j++) {
if (i != j && V[i] + V[j] == input) {
return true;
}
}
}
return false;
}
bool search(const vector<int> V, int input) {
//Linearly searches for a value
for (size_t i = 0; i < V.size(); i++) {
if (V[i] == input) {
return true;
}
}
return false;
}
void unique(vector<int>& V) {
//Uniquify but modifies in place
for (size_t i = 1; i < V.size(); i++) {
//If equal to the previous term...
if (V[i] == V[i-1]) {
}
}
}