Chapter 06 · Week 6

Collections — Arrays and Strings

"The grass withers, the flower fades, but the word of our God will stand forever." — Isaiah 40:8
6

Hold the data in your hands first

The dataset Project 6 asks you to build is real. Search, sort, and filter the real manuscripts below before you write the code to do it.

Manuscript Database — The Real Data

Real Greek New Testament manuscripts. Search by name, filter by type, click any column header to sort. The same operations Project 6 will ask you to implement by hand in C++.

Filter:
Manuscript Century Type Contents Location

For comparison — manuscripts of other ancient works

Greek New Testament (c. AD 50–95)
~5,800
Homer's Iliad (c. 800 BC)
~1,757
Demosthenes' Orations (c. 350 BC)
~340
Tacitus' Annals (c. AD 100)
~33
Caesar's Gallic Wars (c. 50 BC)
~251
What this doesn't argue: manuscript count alone doesn't establish inspiration, doctrinal correctness, or textual purity. Variants exist — well-documented ones. What it does show is that the New Testament is unusually well-attested as a piece of ancient literature. The data is legible. The argument starts there.

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's list — 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.

Check Your Reps

Arrays & Strings — Quick Check

Question 1 of 4
What's the valid index range for int arr[10]?
Why: C++ arrays are 0-indexed. 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.
Question 2 of 4
You have string roster[20] and int count = 5. What's the right loop to print only the in-use names?
Why: The logical size is count (5), not MAX (20). Iterating to MAX prints empty/garbage slots. Iterating with <= count goes one past the last valid index. Use < count.
Question 3 of 4
About how many Greek New Testament manuscripts are catalogued today?
Why: Roughly 5,800 Greek manuscripts. Plus tens of thousands more in Latin, Coptic, Syriac, Armenian, and other languages. The widget above lists a representative sample — the actual database is far larger. By comparison, Homer's Iliad — the next-best-attested classical work — has about 1,757.
Question 4 of 4
Which line correctly defines a constant array size that you can use as the array's declared size?
Why: C++ requires the size of a fixed-size array to be known at compile time. 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).
YOU FINISHED. NICE WORK.

← WEEK 5: FUNCTIONS   ·   WEEK 7: STRUCTS →