Skip to content

Commit 42b0f0d

Browse files
committed
Added all materail for Section07.
1 parent 565e9fe commit 42b0f0d

File tree

8 files changed

+256
-7
lines changed

8 files changed

+256
-7
lines changed

Section07-Arrays_and_Vectors/README.md

+83
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,86 @@ vector <double> high_temperature (365, 80.0); // 365 initializes size of vecto
172172
173173
## Accessing and Modigying Vector Elements
174174
175+
### Array syntax
176+
177+
- Same as arrays
178+
- No bounds checking will be done
179+
180+
`vector_name [element_index]`
181+
182+
```c++
183+
vector <int> test_scores {100, 95, 99, 87, 88};
184+
185+
cout << "First score at index 0: " << test_scores[0] << endl;
186+
cout << "Second score at index 1: " << test_scores[1] << endl;
187+
cout << "Third score at index 2: " << test_scores[2] << endl;
188+
cout << "Fourth score at index 3: " << test_scores[3] << endl;
189+
cout << "Fifth score at index 4: " << test_scores[4] << endl;
190+
```
191+
192+
### Vector syntax
193+
194+
- Created a vector, which is an **object**
195+
- Followed by the **dot** (`.`) operator
196+
- Followed by the **method name**, name of the operation we want performed
197+
198+
`vector_name.at(element_index)`
199+
200+
```c++
201+
vector <int> test_scores {100, 95, 99, 87, 88};
202+
203+
cout << "First score at index 0: " << test_scores.at(0) << endl;
204+
cout << "Second score at index 1: " << test_scores.at(1) << endl;
205+
cout << "Third score at index 2: " << test_scores.at(2) << endl;
206+
cout << "Fourth score at index 3: " << test_scores.at(3) << endl;
207+
cout << "Fifth score at index 4: " << test_scores.at(4) << endl;
208+
```
209+
210+
### Changing the contents of vector elements - vector syntax
211+
212+
- Similar to arrays
213+
214+
`vector_name.at(element_inded)`
215+
216+
```c++
217+
vector <int> test_scores {100, 95, 99, 87, 88};
218+
219+
cin >> test_scores.at(0);
220+
cin >> test_scores.at(1);
221+
cin >> test_scores.at(2);
222+
cin >> test_scores.at(3);
223+
cin >> test_scores.at(4);
224+
225+
// assignment statment
226+
test_scores.at(0) = 90;
227+
```
228+
229+
- but can change size dynamically
230+
231+
`vector_name.push_back(element)`
232+
233+
```c++
234+
vector <int> test_scores {100, 95, 99}; // size is 3
235+
236+
test_scores.push_back(80); // size of 4: 100, 95, 99, 80
237+
test_scores.push_back(90); // size of 5: 100, 95, 99, 80, 90
238+
// vector will automatically allocate the required space!!!
239+
```
240+
241+
- What if you are out of bounds?
242+
- Arrays never do bounds checking
243+
- Many vector methods provide bound checking
244+
- An exception and error message is generated
245+
246+
```c++
247+
vector <int> test_scores {100, 95};
248+
249+
cin >> test_scores.at(5);
250+
```
251+
output error...
252+
253+
```shell
254+
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 5) >= this->size() (which is 2)
255+
This application has requested the Runtime to terminate it in an unusual way.
256+
Please contact the applications support team for more infromation
257+
```

Section07-Arrays_and_Vectors/section07_coding_exercise/README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,28 @@ vector<int> use_array() {
2424
}
2525
```
2626

27-
# Coding Exercise 5: Declaring, Initializing and Accessing Vectors
27+
# Coding Exercise 5: Declaring, Initializing and Accessing Vectors
28+
29+
- Decalre a vector of integers named `vec` and initialize the vector to `10, 20, 30, 40, and 50`
30+
- Modify the first element of the vector to be `100` and modigy the last element of the vector to be `1000`.
31+
- The final vector should then be `100, 20, 30, 40, and 1000`.
32+
33+
## Solution
34+
35+
```c++
36+
#include <vector>
37+
using namespace std;
38+
39+
vector<int> use_vector() {
40+
//----WRITE YOUR CODE BELOW THIS LINE----
41+
42+
vector <int> vec {10, 20, 30, 40, 50};
43+
44+
vec.at(0) = 100;
45+
vec.at(4) = 1000;
46+
47+
//----WRITE YOUR CODE ABOVE THIS LINE----
48+
//----NO NOT MODIFY THE CODE BELOW THIS LINE----
49+
return vec;
50+
}
51+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
1. Arrays are always indexed starting at subscript _____ .
2+
- [x] 0
3+
- [ ] 1
4+
- [ ] "start"
5+
- [ ] "begin"
6+
7+
2. How many integers can the array named numbers contain? `int numbers[10]`
8+
- [ ] 11 (got it wrong the first time... thought it was 11)
9+
- [ ] 9
10+
- [x] 10
11+
- [ ] 0
12+
13+
3. Which of the following is a legal array definiton in C++?
14+
- [ ] `int numbers[0];`
15+
- [ ] `double 5numbers[5];`
16+
- [ ] `real numbers[5];`
17+
- [x] `float numbers[10];`
18+
19+
4. C++ treats an array name as:
20+
- [ ] all of the array elements
21+
- [ ] a variable
22+
- [x] the location in memory of first array element
23+
- [ ] the first array element
24+
25+
5. All array elements must be:
26+
- [ ] enclosed in square brackets `[]`
27+
- [ ] constants
28+
- [ ] literals
29+
- [x] of the same type
30+
31+
6. Given the following array declaration, how would you display `100.7` from the array? `double temperatures[] = {98.6, 95.2, 88.7, 100.7, 89.0};`
32+
- [ ] `cout << temperatures[4] << endl;`
33+
- [ ] `cout << 100.7 << endl;`
34+
- [ ] `cout << temps[3] << endl;`
35+
- [x] `cout << temperatures[3] << endl;`
36+
37+
7. Given the following array declaration, what would the following code display?
38+
39+
```c++
40+
double temperatures[] = {98.6, 95.2, 88.7, 100.7, 89.0};
41+
cout << temperatures[5] << endl;
42+
```
43+
- [x] garbage data
44+
- [ ] 89.0
45+
- [ ] it won't compile
46+
- [ ] it will generate an "out of range error"
47+
48+
*I got this one wrong!! I thought it was either "it won't compile" or "it will generate an 'out of range error'"...*
49+
50+
8. How would you define a vector named temperatures that contains doubles?
51+
- [x] vector <double> temperatures;
52+
- [ ] vector double temperatures;
53+
- [ ] vector [double] temperatures;
54+
- [ ] double vector temperatures
55+
56+
9. How would you determine the number of elements contained in a vector named temperatures?
57+
- [ ] length(temperatures);
58+
- [ ] tmeperatures.length();
59+
- [x] temperatures.size();
60+
- [ ] temperatures.num_elements();
61+
62+
10. What is one way that vectors and arrays are different?
63+
- [ ] vectors always more efficient than arrays
64+
- [ ] arrays can only store primitive types
65+
- [x] vectors can vary their capacity as the program runs, but arrays are fixed in size
66+
- [ ] vectors elements cannot be accessed using their subscript
Binary file not shown.

Section07-Arrays_and_Vectors/section07_source_code/Challenge/main.cpp

+57-1
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,66 @@
3030
*/
3131

3232
#include <iostream>
33+
#include <vector>
34+
35+
using namespace std;
3336

3437
int main() {
3538

36-
// your solution goes here
39+
// Declare 2 empty vectors of integers named vector1 and vector 2, respectively.
40+
vector <int> vector1(0);
41+
vector <int> vector2(0);
42+
43+
// Add 10 and 20 to vector1 dynamically using push_back
44+
vector1.push_back(10);
45+
vector1.push_back(20);
46+
47+
// Display the elements in vector1 using the at() method as well as its size using the size() method
48+
cout << "Display the elements in vector1" << endl;
49+
cout << "Index 0: " << vector1.at(0) << endl;
50+
cout << "Index 1: " << vector1.at(1) << endl;
51+
cout << "Size: " << vector1.size() << endl << endl;
52+
53+
54+
// Add 100 and 200 to vector2 dynamically using push_back
55+
vector2.push_back(100);
56+
vector2.push_back(200);
57+
58+
// Display the elements in vector2 using the at() method as well as its size using the size() method
59+
cout << "Display the elements in vector2" << endl;
60+
cout << "Index 0: " << vector2.at(0) << endl;
61+
cout << "Index 1: " << vector2.at(1) << endl;
62+
cout << "Size: " << vector2.size() << endl << endl;
63+
64+
// Declare an empty 2D vector called vector_2d
65+
vector <vector <int>> vector_2d{};
66+
67+
// Add vector1 to vector_2d dynamically using push_back
68+
// Add vector2 to vector_2d dynamically using push_back
69+
vector_2d.push_back(vector1);
70+
vector_2d.push_back(vector2);
71+
72+
// Display the elements in vector_2d using the at() method
73+
cout << "Display the elements in vector_2d" << endl;
74+
cout << "First row: " << vector_2d.at(0).at(0) << ", " << vector_2d.at(0).at(1) << endl;
75+
cout << "Second row: " << vector_2d.at(1).at(0) << ", " << vector_2d.at(1).at(1) << endl << endl;
76+
77+
78+
// Change vector1.at(0) to 1000
79+
vector1.at(0) = 1000;
80+
cout << "Changed vector1 first element from 10 to 1000" << endl << endl;
81+
82+
// Display the elements in vector_2d again using the at() method
83+
cout << "Display the elements in vector_2d" << endl;
84+
cout << "First row: " << vector_2d.at(0).at(0) << ", " << vector_2d.at(0).at(1) << endl;
85+
cout << "Second row: " << vector_2d.at(1).at(0) << ", " << vector_2d.at(1).at(1) << endl << endl;
86+
cout << "vector1 value did not change inside vector_2d because new values were never passed in..." << endl << endl;
87+
88+
// Display the elements in vector1
89+
cout << "Check vector1 new elements" << endl;
90+
cout << "Index 0: " << vector1.at(0) << endl;
91+
cout << "Index 1: " << vector1.at(1) << endl;
92+
cout << "Size: " << vector2.size() << endl << endl;
3793

3894
cout << endl;
3995
return 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.main</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>

Section07-Arrays_and_Vectors/section07_source_code/Vectors/main.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ int main() {
1212
// vector <char> vowels (5); // 5 initialized to zero
1313
vector <char> vowels {'a' , 'e', 'i', 'o', 'u'};
1414

15-
cout << vowels[0] << endl;
16-
cout << vowels[4] << endl;
15+
cout << vowels[0] << endl; // vowels is a vector but using array syntax so no bound checking is performed
16+
cout << vowels[4] << endl; // vowels is a vector but using array syntax so no bound checking is performed
1717

18-
// vector <int> test_scores (3); // 3 elements all initialized to zero
18+
// vector <int> test_scores (3); // 3 elements all initialized to zero
1919
// vector <int> test_scores (3, 100); // 3 elements all initialized to 100
2020

21-
vector <int> test_scores {100, 98, 89};
21+
vector <int> test_scores {100, 98, 89};
2222

2323
cout << "\nTest scores using array syntax:" << endl;
2424
cout << test_scores[0] << endl;
@@ -30,7 +30,7 @@ int main() {
3030
cout << test_scores.at(0) << endl;
3131
cout << test_scores.at(1) << endl;
3232
cout << test_scores.at(2) << endl;
33-
cout << "\nThere are " << test_scores.size() << " scores in the vector" << endl;
33+
cout << "\nThere are " << test_scores.size() << " scores in the vector" << endl; // size method
3434

3535
cout << "\nEnter 3 test scores: ";
3636
cin >> test_scores.at(0);

0 commit comments

Comments
 (0)