Chapter roadmap
Store collections, traverse them safely, use dynamic vectors, process text, and apply standard algorithms. Work through the concepts in order, type every example yourself, and keep a short debugging log that records predictions, errors, fixes, and what each fix taught you.
Learning objectives
- Use indexed arrays safely
- Choose arrays or vectors
- Traverse with range-based loops
- Transform and search strings and collections
05.1
Fixed-size arrays and indexes
An array stores a fixed number of same-type values contiguously. Indexes begin at zero and the final valid index is size minus one. Accessing outside the array is undefined behavior, so every index must be proven valid.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Example#include <array>
std::array<int, 5> scores{88, 94, 72, 100, 85};
for (std::size_t i = 0; i < scores.size(); ++i) {
std::cout << i << ": " << scores[i] << "\n";
}Result0: 88
1: 94
2: 72
3: 100
4: 85
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
05.2
Vectors and dynamic size
std::vector manages a sequence whose size can change. push_back adds an element, size reports the current count, and at performs checked access. Vectors should be the default sequence container when a fixed size is not a meaningful part of the problem.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Example#include <vector>
std::vector<std::string> tasks;
tasks.push_back("Read");
tasks.push_back("Practice");
tasks.push_back("Reflect");
std::cout << tasks.at(1);ResultPractice
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
05.3
Traversal, search, and transformation
Range-based for loops visit every element without manual indexes. Use a reference when modifying elements and a const reference when reading expensive objects. Algorithms such as sort, find, count, and reverse express common operations clearly.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Example#include <algorithm>
#include <vector>
std::vector<int> values{7, 2, 9, 2};
std::sort(values.begin(), values.end());
for (int value : values) std::cout << value << " ";Result2 2 7 9
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
05.4
String processing
std::string is a sequence of characters with useful operations for size, searching, slicing, and replacement. Strings can be traversed like other containers. getline reads a full line including spaces, while std::cin with >> stops at whitespace.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Examplestd::string phrase = "room three ten";
int spaces = 0;
for (char character : phrase) {
if (character == ' ') ++spaces;
}
std::cout << "Words: " << spaces + 1;ResultWords: 3
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
Chapter assignments
Complete these in order. Later assignments assume that earlier skills are working. Do not only test the sample values; design tests that challenge boundaries, missing values, incorrect types, empty collections, and other likely failures.
- A5.1
Read ten temperatures into an array and report values above and below the average.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A5.2
Build a vector-based to-do list supporting add, remove, list, search, and clear operations.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A5.3
Analyze a sentence for vowels, consonants, digits, spaces, word count, and longest word.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A5.4
Sort a vector of names case-sensitively, search for a requested name, and report duplicate counts.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
Chapter project
Gradebook
Gradebook โ Maintain student names and multiple scores using vectors. Support adding students, recording scores, calculating individual and class statistics, sorting rankings, and searching by name. Separate storage logic from user-interface functions.
Required process
- Write a short specification listing inputs, outputs, rules, and failure cases.
- Break the work into small functions, queries, modules, or classes appropriate to the language.
- Build the smallest working version before adding optional features.
- Test normal, boundary, empty, and invalid cases and record the results.
- Refactor names and duplication, then write a concise user guide.
Key terms
- array
- index
- undefined behavior
- vector
- iterator
- range-based loop
- algorithm
- string
- contiguous