-
Notifications
You must be signed in to change notification settings - Fork 0
/
VECTORS.cpp
142 lines (84 loc) · 2.39 KB
/
VECTORS.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//1/8: Introduction to Vectors
//2/8: Creating a Vector
#include <iostream>
#include <vector>
int main() {
std::vector<double> subway_adult;
// Declare another vector here:
std::vector<double> subway_child;
}
//3/8: Initializing a Vector
#include <iostream>
#include <vector>
int main() {
std::vector<double> subway_adult = {800, 1200, 1500};
// Give subway_child some values:
std::vector<double> subway_child = {400, 600, 750};
}
//4/8: Index
#include <iostream>
#include <vector>
int main() {
std::vector<double> subway_adult = {800, 1200, 1500};
std::vector<double> subway_child = {400, 600, 750};
// What number at index 2 of subway_child?
std::cout << subway_child[2];
}
//5/8: Adding and Removing Elements
#include <iostream>
#include <vector>
int main() {
std::vector<std::string> last_jedi;
// Add characters here:
last_jedi.push_back("kylo");
last_jedi.push_back("rey");
last_jedi.push_back("luke");
last_jedi.push_back("finn");
std::cout << last_jedi[0] << " ";
std::cout << last_jedi[1] << " ";
std::cout << last_jedi[2] << " ";
std::cout << last_jedi[3] << " ";
}
//6/8: .size()
#include <iostream>
#include <vector>
int main() {
std::vector<std::string> grocery = {"Hot Pepper Jam", "Dragon Fruit", "Brussel Sprouts"};
// Add more
grocery.push_back("Artichoke");
grocery.push_back("Tomato");
std::cout << grocery.size();
}
//7/8: Operations
#include <iostream>
#include <vector>
int main() {
std::vector<double> delivery_order;
delivery_order.push_back(8.99);
delivery_order.push_back(3.75);
delivery_order.push_back(0.99);
delivery_order.push_back(5.99);
double total = 0.0;
// Calculate the total using a for loop:
for (int i=0; i<delivery_order.size(); i++){
total += delivery_order[i];
}
std::cout << "Total: $" << total << "\n";
}
//8/8: Review
#include <iostream>
#include <vector>
int main() {
int total_even = 0;
int product_odd = 1;
std::vector<int> vector = {2, 4, 3, 6, 1, 9};
for (int i = 0; i < vector.size(); i++) {
if (vector[i] % 2 == 0) {
total_even = total_even + vector[i];
} else {
product_odd = product_odd * vector[i];
}
}
std::cout << "Sum of even: " << total_even << "\n";
std::cout << "Product of odd: " << product_odd;
}