Programming Fundamentals Using C++

Case Studies · Tier 1 · Beginner

Lectures L01–L12 · Modules 1–6 · think ~5 minutes · paper only. Cases ascend in difficulty within the tier. Hints are progressive — reveal one at a time, on request. Worked solutions: instructor area only.


PF-CS-001 · The Vending Machine

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

  1. You need one variable for the accumulated total.
  2. The stopping condition compares that total against 45.
  3. Change = total − 45; convince yourself any legal total makes it a multiple of 5.

</details>


PF-CS-002 · Three-Cup Order

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

  1. Compare A and B; the smaller one can never be the overall maximum.
  2. Then compare the winner's loser against C for the minimum.
  3. Three comparisons always suffice; two cannot order three unknowns.

</details>


PF-CS-003 · The Receipt Printer

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

  1. Three fields, three types: string, double, int.
  2. The line total multiplies the two numeric fields.
  3. Fixed-point formatting uses two digits after the decimal point.

</details>


PF-CS-004 · Elevator Logic

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

  1. 2×2 = 4 combinations, but two of them are the same outcome.
  2. Equality of the inputs is one explicit branch.
  3. Two moves plus one stay covers all four cells.

</details>


PF-CS-005 · The Grading Curve Debate

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

  1. An if/else-if ladder stops at the first true branch.
  2. With >= tests, the largest threshold must be tested first.
  3. Try score = 95 against the reversed ladder.

</details>


PF-CS-006 · Parking Fee

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

  1. Hours from 1 to 60 minutes both bill as one hour — think of a ceiling.
  2. Billed = hours − 1 for hours beyond the first.
  3. Compare naive fee against 200 for a full-day stay.

</details>


PF-CS-007 · Password Strength Meter

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

  1. Length is one test; the other three need a scan over characters.
  2. <cctype> provides classification functions per character.
  3. Boolean flags can stop a loop early once a category is found.

</details>


PF-CS-008 · The Countdown Timer

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

  1. mm:ss is just seconds/60 and seconds%60 with two-digit padding.
  2. The loop variable is the remaining seconds itself.
  3. A for-loop with a decrementing counter, ending at 0 inclusive.

</details>


PF-CS-009 · Coin Split for Pocket Money

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

  1. Integer division and remainder do all the work.
  2. After 25s, the remainder is < 25 — then apply 10s, then 5s.
  3. Each coin divides the next smaller one's structure: the set is "friendly".

</details>


PF-CS-010 · The Secret Number Hunt

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

  1. Track the range [lo, hi] of possible secrets.
  2. Halve the range every guess by choosing its middle.
  3. log₂(100) ≈ 6.64, so 7 halvings always suffice.

</details>


PF-CS-011 · Traffic Light Cycle

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

  1. The cycle repeats every 55 seconds.
  2. The remainder locates you inside one cycle.
  3. Map remainder ranges: 0–29, 30–34, 35–54.

</details>


PF-CS-012 · Rainfall Logger

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

  1. Total, max, dry, and day counter all persist across iterations.
  2. Initialize max from the first reading, not from 0... or can 0 be right?
  3. Update max when reading > max or reading == max on a later day — decide the tie rule first.

</details>


PF-CS-013 · The Cashier's Change Maker

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

  1. A parallel array of denominations lets one loop serve all coins.
  2. After handling coin c, the remaining amount is < c.
  3. Greedy is minimal here because each denomination is a multiple-sum of smaller ones.

</details>


PF-CS-014 · Class Attendance Percentages

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

  1. Percentage = count/16 × 100 — both operands' types decide the result.
  2. static_cast to double before dividing.
  3. Two accumulators: one per-student inside the loop, one class-level outside.

</details>


PF-CS-015 · The Left-to-Right Largest

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

  1. Keep the running maximum; compare each new height to it.
  2. A tie is not a new record — decide strictly greater.
  3. One pass, O(n): no second loop needed.

</details>


PF-CS-016 · Sum Until the Sentinel

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

  1. The loop condition tests the most recently read value.
  2. Priming read: read once before the loop starts.
  3. Update the variables inside the loop body, after the check.

</details>


PF-CS-017 · The Pyramid Builder

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

  1. Row i (1-based) prints h − i spaces then i stars.
  2. Outer loop rows; two inner loops: spaces, then stars.
  3. Verify the formula on h = 1 and h = 3 by hand first.

</details>


PF-CS-018 · Bus Fare Zoner

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

  1. The branch only decides how much of the per-km rate applies.
  2. Excess = (km > 5) ? km − 5 : 0 — the conditional operator.
  3. Fare = 20 + 5 × max(0, km − 5).

</details>


PF-CS-019 · The Recipe Scaler

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

  1. Scale factor = k/4.0 — one cast keeps the math exact.
  2. Compute in double, round only at print time.
  3. nearest 5 = 5 × floor(x/5 + 0.5).

</details>


PF-CS-020 · Exam Averages with an Absence Rule

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

  1. Sum only scores ≥ 0; count them too.
  2. Guard division: count == 0 means a flag, not a number.
  3. Comparability is a policy decision: state it, don't hide it.

</details>

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