Algorithms & Flowcharts — Worked Examples
12. Everyday Flowcharts: Algorithms in Real Life
Algorithms you already follow
You run algorithms every day without calling them that: using an ATM, logging in, buying a snack, crossing at the lights. Each is a sequence of steps with decisions (branches) and sometimes loops (repeating until something is true). Drawing them as flowcharts makes the hidden logic — especially the "what if it goes wrong?" branches — visible. Watch the Yes/No labels: getting a branch backwards is the most common bug.
(a) ATM cash withdrawal
Problem
A customer inserts a card and asks for cash. The machine must check the PIN, check the balance, and only then dispense money.
Pseudocode
ATM_WITHDRAW:
pin = INPUT
IF pin is not valid:
show "Incorrect PIN"
ELSE:
amount = INPUT
IF amount <= balance:
dispense(amount)
balance = balance - amount
print receipt
ELSE:
show "Insufficient funds"
In Python
def atm_withdraw(entered_pin, correct_pin, amount, balance):
if entered_pin != correct_pin:
return "Incorrect PIN", balance
if amount > balance:
return "Insufficient funds", balance
balance -= amount
return f"Dispensed {amount}", balance
print(atm_withdraw("1234", "1234", 60, 200))
Output:
('Dispensed 60', 140)
(b) Website login with a maximum of 3 attempts
Problem
Let a user try to log in. Allow at most three wrong passwords, then lock the account.
Pseudocode
LOGIN:
tries = 0
WHILE tries < 3:
pw = INPUT
IF pw is correct:
grant access
STOP
ELSE:
tries = tries + 1
lock account
Trace table
User types "abc", "xyz", then the correct "pass" on the third try:
| tries (before) | tries < 3? | password | correct? | result |
|---|---|---|---|---|
| 0 | Yes | abc | No | tries → 1 |
| 1 | Yes | xyz | No | tries → 2 |
| 2 | Yes | pass | Yes | access granted, stop |
In Python
def login(attempts, correct="pass"):
tries = 0
while tries < 3:
pw = attempts[tries]
if pw == correct:
return "Access granted"
tries += 1
return "Account locked"
print(login(["abc", "xyz", "pass"]))
print(login(["a", "b", "c"]))
Output:
Access granted
Account locked
(c) Vending machine
Problem
A customer selects an item, inserts money a coin at a time, and the machine either gives the item plus change, or refunds if they cancel.
Pseudocode
VENDING_MACHINE:
price = selected item price
paid = 0
WHILE paid < price:
coin = INPUT ' a coin, or "cancel"
IF coin == "cancel":
refund(paid)
STOP
paid = paid + coin
dispense item
IF paid > price:
give change (paid - price)
In Python
def vend(price, coins):
paid = 0
for coin in coins:
if coin == "cancel":
return f"Refund {paid}"
paid += coin
if paid >= price:
return f"Dispense item, change {paid - price}"
return f"Refund {paid}" # ran out of coins
print(vend(75, [25, 25, 25]))
print(vend(75, [25, "cancel"]))
Output:
Dispense item, change 0
Refund 25
(d) Traffic-light controller cycle
Problem
Control a traffic light that repeats forever: Green, then Amber, then Red, each for a fixed time, then back to Green.
Pseudocode
TRAFFIC_LIGHT:
REPEAT:
set light GREEN; wait 30s
set light AMBER; wait 5s
set light RED; wait 30s
UNTIL power is off
In Python
def traffic_cycle(cycles):
sequence = [("GREEN", 30), ("AMBER", 5), ("RED", 30)]
log = []
for _ in range(cycles): # 'cycles' stands in for "while powered on"
for colour, seconds in sequence:
log.append(f"{colour} {seconds}s")
return log
for step in traffic_cycle(2):
print(step)
Output:
GREEN 30s
AMBER 5s
RED 30s
GREEN 30s
AMBER 5s
RED 30s