Collections — Arrays and Strings
"The grass withers, the flower fades, but the word of our God will stand forever." — Isaiah 40:8
Why This Matters
For five chapters you have managed one thing at a time. The only way to handle many
things was to declare many variables: score1, score2,
score3. That breaks down fast.
This chapter introduces collections — single names that hold many values. The simplest collection is the array. With arrays and strings you can manage rosters, manuscripts, evidence catalogs — anything where "how many" is itself a variable.
Arrays: One Name, Many Values
int scores[5] = {90, 85, 72, 88, 95};
cout << scores[0]; // 90
cout << scores[4]; // 95
cout << scores[5]; // ⚠ out of bounds — undefined behavior
Indices start at 0. An array of 5 elements has valid indices 0–4. Asking
for scores[5] is a bug. C++ does not check bounds for you. The compiler will not
warn. You have to be careful.
The Canonical Iteration
const int SIZE = 5;
int scores[SIZE] = {90, 85, 72, 88, 95};
for (int i = 0; i < SIZE; i++) {
cout << scores[i] << endl;
}
Three things to internalize: use a named const for the size; loop condition is
i < SIZE (not <=); the same pattern works for reading,
writing, or accumulating.
Fixed Size, Logical Count
Often the array is bigger than what you're currently using. The standard pattern:
const int MAX = 20;
string roster[MAX];
int count = 0; // how many are actually in use
void add_player(string name) {
if (count < MAX) {
roster[count] = name;
count++;
}
}
When you iterate to display: for (int i = 0; i < count; i++) — not
i < MAX. The array always has MAX slots; you're only using count
of them.
Strings as Collections
Strings work a lot like arrays of characters. name.length(),
name[i], name.substr(start, length), name.find(target).
Use == for string equality (unlike Java's .equals — that's a
Chapter 13 story).
Coach's Note — The "fixed-size array plus a count" pattern is the foundation of every dynamic-size container ever built.
vector,ArrayList, Python'slist— all of them are this pattern with automatic growth. Master the manual version and the libraries make sense.
This Week's Project
You're ready for Project 6: Manuscript Database. A menu-driven program managing real manuscript data — add, remove, list, search (case-insensitive), sort by century (selection sort, by hand). The data is real. Use real values. The grader will check.
Arrays & Strings — Quick Check
int arr[10]?int arr[10] has 10 elements at indices 0, 1, ..., 9. arr[10] is past the end — accessing it is undefined behavior, and the compiler does not catch it. string roster[20] and int count = 5. What's the right loop to print only the in-use names?count (5), not MAX (20). Iterating to MAX prints empty/garbage slots. Iterating with <= count goes one past the last valid index. Use < count. const int SIZE = 5 is a compile-time constant, so int arr[SIZE] works. A plain int SIZE = 5 would be a runtime variable — and most compilers won't accept it as an array size (C99 allowed variable-length arrays; C++ doesn't).