Problem Solving — CS & DS

Lecture 03 — Flowcharts and Control Flow

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

Rain or shine: decisions to diamonds

cs-010

Picnic decision: go if temperature is 18-28 and no rain; if rain but warm, go with a tent; otherwise cancel. Flowchart it, then trace three days.

Expected: go / go with tent / cancel

Cost: Constant; the lesson is diamond wiring, not cost.

Worked example

The ATM loop, arrows and all

cs-009

Cash machine: allow withdrawals while the balance lasts and at most 3 per session; refuse amounts over balance without ending the session.

Expected: refused / balance 30

Cost: O(requests). The flowchart question: draw where the 'refused' arrow re-enters.

Worked example

Dispatch loop: proving termination

cs-011

A dispatch system processes queued jobs; every processed job may enqueue 0-2 smaller sub-jobs. Flowchart the loop and argue it terminates.

Expected: all dispatched

Cost: O(total size). This is the first occurrence of 'the loop ends because something provably shrinks'.

Worked example

Guess and check as a loop

cs-012

Number game: the program holds a secret 1-50; the player guesses; feedback 'higher'/'lower'/'correct'. Flowchart the game loop including a maximum of 7 guesses.

rain = True
tent = False
if rain:
    if tent:
        print("go with tent")
    else:
        print("cancel")
else:
    print("go")

Expected: higher / lower / correct

Cost: At most 7 feedback rounds by the rules; a halving strategy needs ceil(log2(50)) = 6.

Case attempt · lecture 03
Case cs-009 · Beginner

The ATM Loop

Draw a flowchart for an ATM session: PIN with three attempts, then a menu (balance, withdraw, quit) with a balance check on withdrawal.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-009 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-009 — not embedded in this deck.

Case attempt · lecture 03
Case cs-010 · Foundational

Rain or Shine

A garden watering system turns on based on rain sensor, time of day, and soil moisture. Build the decision flowchart and simplify redundant branches.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-010 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-010 — not embedded in this deck.

Case attempt · lecture 03
Case cs-144 · Foundational

Two Paths, One Post

The careers office wants an automated reply for internship posts. Two staff members wrote requirement notes:

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-144 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-144 — not embedded in this deck.

Case attempt · lecture 03
Case cs-011 · Intermediate

The Dispatch Loop

A delivery scooter loops: take next order, deliver, return. Model it as a flowchart with a clear termination condition and what happens when orders run out mid-shift.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-011 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-011 — not embedded in this deck.

Case attempt · lecture 03
Case cs-012 · Advanced

Guess and Check

A number-guessing game with 'higher/lower' feedback. Flowchart the loop and argue how many attempts it can take before the loop must end.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-012 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-012 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Every decision diamond must have how many exits, and how labelled?
  2. What is the standard proof that a loop terminates?
  3. What is a dead branch and how do flowcharts expose one?
Expected answers (instructor): exactly two, labelled with mutually exclusive conditions name a quantity that strictly decreases (or increases) and is bounded an unreachable path; exposed by tracing arrows from start with all conditions
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF