-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection7-challenge.cpp
More file actions
109 lines (75 loc) · 3.73 KB
/
section7-challenge.cpp
File metadata and controls
109 lines (75 loc) · 3.73 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
/* Section 7
Challenge
Write a C++ program as follows:
Declare 2 empty vectors of integers named
vector1 and vector 2, respectively.
Add 10 and 20 to vector1 dynamically using push_back
Display the elements in vector1 using the at() method as well as its size using the size() method
Add 100 and 200 to vector2 dynamically using push_back
Display the elements in vector2 using the at() method as well as its size using the size() method
Declare an empty 2D vector called vector_2d
Remember, that a 2D vector is a vector of vector<int>
Add vector1 to vector_2d dynamically using push_back
Add vector2 to vector_2d dynamically using push_back
Display the elements in vector_2d using the at() method
Change vector1.at(0) to 1000
Display the elements in vector_2d again using the at() method
Display the elements in vector1
What did you expect? Did you get what you expected? What do you think happended?
*/
#include <iostream>
#include <vector>
using std::vector;
int main()
{
// Declare 2 empty vectors of integers named
// vector1 and vector 2, respectively.
vector <int> vector1 {}; // {} can be omitted
vector <int> vector2 {}; // {} can be omitted
// Add 10 and 20 to vector1 dynamically using push_back
// Display the elements in vector1 using the at() method as well as its size using the size() method
vector1.push_back(10);
vector1.push_back(20);
// Display the elements in vector1 using the at() method as well as its size using the size() method
std::cout << "\nvector1 elements:\n";
std::cout << "vector1[0]: " << vector1.at(0) << '\n';
std::cout << "vector1[1]: " << vector1.at(1)<< '\n';
std::cout << "vector1 size: " << vector1.size()<< '\n';
// Add 100 and 200 to vector2 dynamically using push_back
vector2.push_back(100);
vector2.push_back(200);
// Display the elements in vector2 using the at() method as well as its size using the size() method
std::cout << "\nvector2 elements:\n";
std::cout << "vector2[0]: " << vector2.at(0) << '\n';
std::cout << "vector2[1]: " << vector2.at(1)<< '\n';
std::cout << "vector2 size: " << vector2.size()<< '\n';
// Declare an empty 2D vector called vector_2d
// Remember, that a 2D vector is a vector of vector<int>
vector <vector<int>> vector_2d {}; // {} can be omitted
// Add vector1 to vector_2d dynamically using push_back
// Add vector2 to vector_2d dynamically using push_back
vector_2d.push_back(vector1);
vector_2d.push_back(vector2);
// Display the elements in vector_2d using the at() method
std::cout << "\nvector_2d row 0.\n";
std::cout << vector_2d.at(0).at(0) << '\n';
std::cout << vector_2d.at(0).at(1) << '\n';
std::cout << "\nvector_2d row 1.\n";
std::cout << vector_2d.at(1).at(0) << '\n';
std::cout << vector_2d.at(1).at(1) << '\n';
// Change vector1.at(0) to 1000
vector1.at(0) = 1000;
// Display the elements in vector_2d again using the at() method
// vector_2d doesn't pick up change to vector1, it gets a copy of vector at initialisation
std::cout << "\nvector_2d row 0.\n";
std::cout << vector_2d.at(0).at(0) << '\n';
std::cout << vector_2d.at(0).at(1) << '\n';
std::cout << "\nvector_2d row 1.\n";
std::cout << vector_2d.at(1).at(0) << '\n';
std::cout << vector_2d.at(1).at(1) << '\n';
// Display the elements in vector1 using the at() method as well as its size using the size() method
std::cout << "\nvector1 elements:\n";
std::cout << "vector1[0]: " << vector1.at(0) << '\n';
std::cout << "vector1[1]: " << vector1.at(1)<< '\n';
return 0;
}