Programming Fundamentals Using C++

Case Studies · Tier 2 · Foundational

Lectures L13–L16 · Modules 7–8 · think ~5 minutes · paper only. Cases ascend in difficulty within the tier. Worked solutions: instructor area only.


PF-CS-021 · Leap Year Interrogation

<details><summary>Hints (progressive)</summary>

  1. The two "except" clauses combine with && and ||.
  2. (y%4==0 && y%100!=0) || y%400==0 — verify on 1900, 2000, 2024.
  3. A pure function (bool → bool) is testable without I/O.

</details>


PF-CS-022 · The Safe Division Service

<details><summary>Hints (progressive)</summary>

  1. A sentinel can collide with a legal quotient.
  2. Two channels: one return for the value, one reference/bool for validity.
  3. The caller decides policy; the function reports facts.

</details>


PF-CS-023 · Unit Converter with Contracts

<details><summary>Hints (progressive)</summary>

  1. F = C×9/5 + 32; the inverse undoes each operation in reverse order.
  2. Below absolute zero the input is invalid — decide: error channel vs clamped value.
  3. Precondition: C ≥ −273.15; postcondition: |result − expected| < 1e-9.

</details>


PF-CS-024 · The Collatz Witness

<details><summary>Hints (progressive)</summary>

  1. collatzSteps does one job: count steps for a single n.
  2. The scan loop is a different job: track (best n, best count).
  3. Inline the logic and the scan's max-tracking disappears inside step arithmetic — harder to test, harder to read.

</details>


PF-CS-025 · Invoice Splitter

<details><summary>Hints (progressive)</summary>

  1. Compute in cents: total×100 as long long.
  2. Share = cents/people; leftover cents must go somewhere — state the rule.
  3. people ≤ 0 is a caller error: document it, and decide the function's response.

</details>


PF-CS-026 · The Recursive-Looking Password

<details><summary>Hints (progressive)</summary>

  1. Compare s[i] with s[i+1].
  2. i must stop at size()−2 — the classic off-by-one.
  3. An empty or 1-char string has no adjacent pair: false.

</details>


PF-CS-027 · Overload Tournament

<details><summary>Hints (progressive)</summary>

  1. Overloads differ in parameter count or types.
  2. area(int) vs area(double) can collide at call sites with int literals — discuss.
  3. Count of parameters (2 for rect, 1 for circle) is the safest discriminator.

</details>


PF-CS-028 · The Swap Handoff

<details><summary>Hints (progressive)</summary>

  1. By value copies: the caller's originals never move.
  2. Pointer and reference both reach the caller's storage.
  3. References make the call read like the value version but behave like the pointer version — that is exactly their power and their danger.

</details>


PF-CS-029 · Statistics on Demand

<details><summary>Hints (progressive)</summary>

  1. Out-parameters must be initialized by the caller or in the function's guard.
  2. n == 0 must be rejected before reading a[0].
  3. One function keeps the three numbers consistent; three functions risk a stale mix.

</details>


PF-CS-030 · Default Grading Modes

<details><summary>Hints (progressive)</summary>

  1. Default arguments live in the declaration, not per-call.
  2. 89.5 → 90 lenient, 89 strict: the transcript that argues the policy.
  3. Defaults should be the mode that cannot inflate grades silently.

</details>


PF-CS-031 · The Recursive Countdown

<details><summary>Hints (progressive)</summary>

  1. Base case n == 0 prints and returns.
  2. Each call passes n−1: strictly shrinking.
  3. Recursion depth equals n — thousands deep is where the stack complains (implementation-defined limit).

</details>


PF-CS-032 · Recursive Sum of Digits

<details><summary>Hints (progressive)</summary>

  1. digitSum(n) = n%10 + digitSum(n/10).
  2. Base: n < 10 → n.
  3. n/10 < n for all n ≥ 10, so the chain must hit a single digit.

</details>


PF-CS-033 · Tower of Hanoi — Move Counter

<details><summary>Hints (progressive)</summary>

  1. Move n−1 aside, move the big disk, move n−1 back: two subproblems.
  2. hanoi(1) = 1; each level doubles the work.
  3. 2ⁿ − 1; for n = 3 that is 7 moves — count them.

</details>


PF-CS-034 · Function Table Refactor

<details><summary>Hints (progressive)</summary>

  1. Parameters carry the variation: name and multiplier.
  2. Printing functions are easy to call but hard to test; returning values is the reverse.
  3. For this course: compute-and-return, print at the call site.

</details>


PF-CS-035 · The Pass-by-Value Budget

<details><summary>Hints (progressive)</summary>

  1. The parameter is a copy; the mutation dies with it.
  2. Fix A: double& price. Fix B: return the new value.
  3. Fix B forces the call site to show assignment — visible data flow.

</details>


PF-CS-036 · Primes in a Range — Function Anatomy

<details><summary>Hints (progressive)</summary>

  1. If d divides n, so does n/d — one of them is ≤ √n.
  2. Test 2, then odd divisors only.
  3. printPrimes owns I/O; isPrime returns bool — one job each.

</details>


PF-CS-037 · The Bilingual Menu (Overload vs Default)

<details><summary>Hints (progressive)</summary>

  1. Both compile; the difference is at the call site and in maintenance.
  2. A third language under design (b) means another overload; under (a), another branch.
  3. Branching inside one function centralizes policy — usually the winner here.

</details>


PF-CS-038 · Scope Detective

<details><summary>Hints (progressive)</summary>

  1. The innermost declaration wins name lookup.
  2. Shadowing is legal but a readability hazard.
  3. :: reaches the global — a band-aid; renaming is the cure.

</details>


PF-CS-039 · Menu Loop with Function Dispatch

<details><summary>Hints (progressive)</summary>

  1. A while-loop around menu+dispatch; the exit is a condition, not a break-by-default.
  2. switch inside the loop; default handles garbage.
  3. Keep the menu printing inside the loop so every path re-displays it.

</details>


PF-CS-040 · Recursive Binary Strings

<details><summary>Hints (progressive)</summary>

  1. At each position: append '0' or '1', recurse, undo.
  2. Depth = n; the tree has 2ⁿ leaves.
  3. 2²⁰ ≈ 10⁶ lines — printable but pointless; the analysis is the answer.

</details>

Programming Fundamentals Using C++ · C++17 · 16 weeksBack to top ↑