Programming Fundamentals Using C++

L18 · Array Algorithms: Fill, Print, Sum/Average, Min/Max, Count, Linear Search

Module 9 — One-Dimensional Arrays · Week 9 · Lecture 18 of 32 · 120 minutes Outcomes: CLO-6 · PF-9.3, PF-9.4 · LEARNING_OUTCOMES.md · Quiz 2 (Modules 5–8)

Learning objectives

  1. Implement from a blank editor — no notes — the core array algorithms: fill, print, sum/average, min/max (value and index), count-if, and reverse-in-place (PF-9.3).
  2. Implement linear search with the FIND-FIRST pattern (early exit), state its best/worst comparison counts, and write its test plan including the not-found boundary (PF-9.4).
  3. Map each algorithm to its Module 5 pattern name (ACCUMULATE, COUNT-IF, FIND-FIRST) — showing pattern vocabulary carrying new data structures (PF-9.3, PF-9.4).

Prerequisites

L17 (array mechanics); L09–L10 (patterns); L13 (writing functions — algorithms are written as functions from here on).

Concept sequence

  1. Algorithms as functions over arrays (signature design first)
  2. Fill + print (the development pair)
  3. ACCUMULATE: sum, average (with the L05 truncation lesson!)
  4. EXTREMES: min/max with index tracking (two-output via references — L15)
  5. COUNT-IF: predicate counting
  6. FIND-FIRST: linear search with early exit + complexity count

Teaching topics (detailed)

C++ examples required

FileRole
array_algorithms.cpp ✅all six algorithms as functions + a main that exercises them on a fixed test array with printed results
(live) linear_search_trace.cppsearch traced with comparison counter printed per iteration

Conceptual explanation (beginner-first)

Real text is messy: " Ada LOVELACE " needs to become "Ada Lovelace"; a CSV line "12,7,9" needs to become three numbers; a report line needs a number glued into the middle of a sentence. This lecture is a workshop of the small algorithms that do that work — all built from the scanning patterns we already own.

Two standard library helpers earn their keep this week. getline(cin, s) reads a whole line including spaces (whereas cin >> s stops at the first space) — essential for names. And string concatenation with + builds results character by character or piece by piece: s = s + ch grows a string one character at a time. Neither is exotic; both are daily tools.

Terminology and definitions

TermDefinition
getlineReads one full line (spaces included) into a std::string
Concatenation+ or += joining strings; s += ch appends one char
TrimRemoving leading/trailing spaces
Word boundaryA position where a word starts/ends (space or string edge)
Case mappingtoupper/tolower per character via <cctype>
TokenOne whitespace-separated piece of a line
Title caseFirst letter of each word upper, rest lower

Syntax and C++ examples

std::string line;
std::getline(std::cin, line);        // whole line, spaces kept

// count words: a new word starts at a non-space after a space/edge
int words{0};
bool inWord{false};
for (char ch : line)
{
    if (std::isspace(static_cast<unsigned char>(ch)))
        inWord = false;
    else if (!inWord) { ++words; inWord = true; }
}

// build a string piece by piece
std::string cleaned;
for (char ch : line)
    if (!std::isspace(static_cast<unsigned char>(ch)))
        cleaned += ch;               // append one character

Line-by-line code explanation

examples/string_algorithms.cpp (clean/split/build sections):

  1. trimLeading(line) — the classic index-skip: find the first non-space position start, then build the result from substr(start) (or erase in place). Edge case: all-spaces input must yield "", so the not-found case is checked before using start.
  2. Word counter — the inWord flag is a tiny state machine: false means "between words." Entering a non-space while inWord == false is a boundary crossing: count it and flip the flag. Double spaces never double-count because the flag only flips on the space→letter transition, not on every letter.
  3. CSV split — walk the line; on ',' push the token accumulated so far and reset it; after the loop push the final token (the one with no comma after it). Forgetting the post-loop push is the bug.
  4. cleaned += ch builds the space-free version — appending one char per accepted character; contrast with building a new std::string via index arithmetic for the trim.

Output prediction questions (with answers)

  1. " Ada LOVELACE " through the word counter — ? — 2 words (double space handled by the flag).
  2. Trim of " hi " — ? — "hi"; trim of " " — "".
  3. Split of "12,7,9" — ? — {"12", "7", "9"}; of "12,,9" — ? — {"12", "", "9"} — an empty middle token is data, not a bug.
  4. s = "val: "; s += 42; — ? — appends the character with code 42 ('*'), not the digits — the char-vs-int conversion trap.
  5. Title-case of "ada l." — ? — "Ada L."; the first-letter rule fires on the boundary, not on every letter.

Common errors and debugging examples

ErrorSymptomFix
Mixing cin >> s and getlineThe getline gets an empty leftover lineDrain with cin.ignore after >> (L04 rule)
Word counter without the flagDouble spaces count twiceState machine: flag flips on transitions only
Missing final token in splitLast CSV field vanishesPush the accumulator after the loop
s += 42 expecting digitsAsterisk appearss += std::to_string(42)
Off-by-one in trimOne space survives / a letter diesTrace a 2-char string by hand
Assuming length() counts wordsLength counts charactersWords need boundaries, not counts

Common student misconceptions

Classroom demonstrations

  1. Double-space stress test: run the counter on "a b c" live — the flag makes it boring, which is the point.
  2. The vanishing token: run a split missing the post-loop push — "12,7,9" prints only two numbers; students spot the missing push.
  3. Char arithmetic surprise: s += 42 vs s += "42" side by side — the type of the right operand is the whole story.

Guided student activities

  1. Design-your-own trim: pairs write trimTrailing (mirror of the leading version) and exchange with a neighbor to trace.
  2. CSV dialect: modify the splitter to accept ';' — one character changes; discuss what a real CSV parser must additionally handle.
  3. Counting vowels: implement with <cctype> (tolower first), then predict its behavior on "XYZ" vs "xyz" before running.

Practice problems

Practice problems

Summary

String work is scanning plus building: getline for full lines, isspace-gated state machines for words, += to grow results, and index arithmetic to mirror or trim. The word counter's inWord flag is the reusable idea — transitions between "in" and "out" are where events happen. Next (L21): searching arrays — linear and binary — and the first real complexity comparison.

Exit ticket / formative assessment

  1. Write the loop that counts commas in a line — then states why it is COUNT-IF in disguise.
  2. Given line = "ada l." (two spaces), what does the word counter print, and which variable prevents a miscount?
  3. What does the split routine owe to the loop boundary — i.e., why is there a push after the loop?

Estimated time allocation (120 min)

SegmentMinutes
Quiz 215
Signature design + fill/print + accumulate25
Break10
Extremes + count-if + linear search (+complexity count)40
Algorithm relay activity20
Exit ticket + Module 10 preview10
Programming Fundamentals Using C++ · C++17 · 16 weeksBack to top ↑