flow and modularity
inf113
faculty of information
university of toronto
dan ryan

Outcomes

  • Pseudocode2Flowchart
  • Flowchart2Pseudocode
  • Real World ↔ Pseudocode ↔ Flowchart
  • Critique and Fix Ill-formed flowcharts
  • Flowcharts and stepwise refinement
  • Flowcharts and black boxes
  • Flowcharts and modularity

Review Questions

Tutorial

Problem Sets

Is this a great flowshart?

 Image from "Healers of the Wild: People Who Care for Injured and Orphaned Wildlife" by Shannon K. Jacobs, by way of the Wild Bird Fund. 

Flowchart = visualization of sequence and contingency

WHAT FLOWS?

A TO DO List is not an agenda!

Contingency

Wake Up

Shower

Eat

Read

M,W,F

Shake

Egg


Make Breakfast
 

Wake Up

Shower

Eat

Read

M,W,F

Shake

Egg


Make Breakfast
 

Wake Up

Shower

Eat

Read

M,W,F

Shake

Egg


Make Breakfast
 

Wake Up

Shower

Eat

Read

Make Breakfast

M,W,F

Shake

Egg

Wake Up

Shower

Eat

Read

Make Breakfast

M,W,F

Shake

Egg

Wake Up

Shower

Eat

Read

M,W,F

Shake

Egg


Make Breakfast
 

FLOWCHART CONVENTIONS

The Elements of Flow(charts)

arrow

flow

"go to..."

circle

terminal/connector

"and then..."

diamond

decision/branch

"if this is true..."

rectangle

module/action

"do this"

Flow in and Flow Out

Control flows INTO THE TOP and OUT OF THE BOTTOM of rectangles.

Control flows INTO THE TOP and OUT THE SIDES of diamonds.

Control can flow INTO THE TOP, IN FROM THE LEFT or RIGHT and OUT THE BOTTOM of a connector circle.

\

O

\

O

\

O

Exercises

Grammar of Flowcharts

Entry Circle

Exit Circle

Zero or

more

MODULES

module

But what is a module?

Module

action

module

condition

module

condition

module

condition

module

OR

OR

OR

OR

module

action

action

action

action

module

action

action

action

action

module

module

condition

module

condition

module

module

condition

module

condition

module

condition

module

module

condition

module

module

module

condition

module

module

condition

module

condition

module

module

condition

module

condition

module

condition

RULE: Every Module has a Single Entry and a Single Exit

This can be a single action or any combination of sequence and contingency.

MODULE: any chunk of activity that can be popped into or out of a flowchart because of its single entry single exit property.

MODULE

How Does "Single Entry Single Exit" Rule Apply to Decisions?

Diamonds Do Not Stand Alone

condition
A

do action
B

do action
C

if condition A
     do action B
else
     do action C

the diamond splits the

flow into branches

the circle rejoins the

branches into a single exit

Diamond = IF-THEN-ELSE Module

condition
A

do action
B

do action
C

if condition A
     do action B
else
     do action C

Diamond = IF-THEN-ELSE Module

condition
A

do action
B

do action
C

if condition A
     do action B
else
     do action C

Two Special "if-then-else" Modules

While it is raining, keep your umbrella up.

Keep your umbrella up until it stops raining.

"if-then-else" > WHILE Modules

While it is raining, use an umbrella.

If it is raining, use an umbrella, if not, put the umbrella away. Check every few minutes to see if it is still raining.

Is it raining?

    Yes? Use an umbrella

    Check again

Put the umbrella away

raining?

use umbrella

put umbrella away

"if-then-else" > UNTIL Modules

Use an umbrella. Check every few minutes to see if it is still raining.  When it stops, put the umbrella away.

Use an umbrella

    Check for rain

    If no rain, proceed.

Put the umbrella away

use umbrella

put umbrella away

Keep your umbrella up until it stops raining.

stopped raining?

Note: We Now Have Flowchart for Repetition

WHILE A DO B

A?

B

YES

NO

SINGLE EXIT POINT

SINGLE ENTRY POINT

New Concept: Flowchart for Repetition

REPEAT B UNTIL A

A?

B

SINGLE EXIT POINT

SINGLE ENTRY POINT

NO

YES

In a WHILE loop, the "action" may be executed ZERO OR MORE TIMES

In an UNTIL loop, the "action" is always executed AT LEAST ONCE (i.e., "one or more times")

Practice

How to find wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

wildlife rehab

fledgling

Y

Y

Y

Y

Y

Y

Y

Y

Q: How would you redraw this flowchart in "standard" form?

How to find wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

wildlife rehab

fledgling

Y

Y

Y

Y

Y

Y

Y

Y

How to find wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

wildlife rehab

fledgling

Y

Y

Y

Y

Y

Y

Y

Y

How to find wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

wildlife rehab

fledgling

Y

Y

Y

Y

Y

Y

Y

How to find wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

wildlife rehab

fledgling

Y

Y

Y

Y

Y

Y

Y

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

How to find wildlife rehab

wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

How to find wildlife rehab

wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

How to find wildlife rehab

wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

wildlife rehab

How to find wildlife rehab

wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

wildlife rehab

How to find wildlife rehab

wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

wildlife rehab

How to find wildlife rehab

wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

wildlife rehab

How to find wildlife rehab

wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

wildlife rehab

How to find wildlife rehab

wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

wildlife rehab

How to find wildlife rehab

wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

fledgling

Y

Y

Y

Y

Y

Y

Y

wildlife rehab

wildlife rehab

How to find wildlife rehab

wildlife rehab

How to find wildlife rehab

hurt or sick?

feathered?

nestling

intact?

see nest?

safe?

put in nest

parents?

make nest

put in nest

parents?

leave

leave

put in bushes

parents?

leave

wildlife rehab

wildlife rehab

leave

wildlife rehab

fledgling

Y

Y

Y

Y

Y

Y

Y

wildlife rehab

wildlife rehab

Example 1

If today is Tuesday, we must be in Belgium

Tuesday?

Belgium

YES

?

Example 2

DO A. 
IF B, 
   DO C, 
   otherwise DO D. 
DO E. 
DO F.

B

A

E

F

C

D

YES

NO

IF B

DO C

OTHERWISE D

IF B, 
   DO C, 
   otherwise DO D. 

Example 2

DO A. 
IF B, 
   DO C, 
   otherwise DO D. 
DO E. 
DO F.

B

A

E

F

C

D

YES

NO

IF B

DO C

OTHERWISE D

IF B, 
   DO C, 
   otherwise DO D. 

CRITIQUE

FIX

EX 6

EX 2

if raining
    then if umbrella down, raise umbrella
otherwise
    if umbrella is up lower umbrella

EX 3

Q1020.

I scored an amazing summer practicum washing pots in a restaurant. My boss is a computational thinker and wants me to post a flowchart describing the pot-washing process on the wall behind the pot-washing sinks. Here are her instructions:

Scrub until clean. Rinse until clear. Inspect. If not OK, start over.

Proceed Stepwise: (1) Pseudocode; (2) Outside; (3) Inside

scrub
until clean
rinse
until clear
inspect
if OK, done
else, start over
repeat
    repeat
       scrub
    until clean
    repeat
       rinse
    until clear
    inspect
until OK

EX 5

Q1020.

I scored an amazing summer practicum washing pots in a restaurant. My boss is a computational thinker and wants me to post a flowchart describing the pot-washing process on the wall behind the pot-washing sinks. Here are her instructions:

Scrub until clean. Rinse until clear. Inspect. If not OK, start over.
scrub
until clean
rinse
until clear
inspect
if OK, done
else, start over
repeat
    repeat
       scrub
    until clean
    repeat
       rinse
    until clear
    inspect
until OK

Q1020.

I scored an amazing summer practicum washing pots in a restaurant. My boss is a computational thinker and wants me to post a flowchart describing the pot-washing process on the wall behind the pot-washing sinks. Here are her instructions:

Scrub until clean. Rinse until clear. Inspect. If not OK, start over.

Q1020.

                      I scored an amazing summer practicum washing pots in a restaurant. My boss is a computational thinker and wants me to post a flowchart describing the pot-washing process on the wall behind the pot-washing sinks. Here are her instructions:

Scrub until clean. Rinse until clear. Inspect. If not OK, start over.

scrub
until clean
rinse
until clear
inspect
if OK, done
else, start over
repeat
    repeat
       scrub
    until clean
    repeat
       rinse
    until clear
    inspect
until OK

EX 5

Title Text

EX 7

EX 7

EX 8

EX 8

Part 2: Solving Problems with Flow and Modularity

Sketch flow chart that captures logic of the following process.

Organization consists of intake personnel, counselors, followup social workers, and clerical staff.

When a new client contacts the organization intake personnel determine which of four types of case it is by asking two questions.

 If a client is returning having already been "typed" she is sent to the appropriate waiting room.

Types 1 and 3 are referred to counselor A, type 2 to counselor B, type 4 to counselor C.

Client goes to waiting room until counselor is free.

Sessions take 1 hour so the wait can be long. If more than one person waiting client is advised to go away and come back later.

In session, if the client is over 18 they get treatment protocol 1 otherwise they get treatment protocol 2.

Sketch flow chart that captures logic of the following process.

Organization consists of intake personnel, counselors, followup social workers, and clerical staff.

When a new client contacts the organization intake personnel determine which of four types of case it is by asking two questions.

 If a client is returning having already been "typed" she is sent to the appropriate waiting room.

Types 1 and 3 are referred to counselor A, type 2 to counselor B, type 4 to counselor C.

Client goes to waiting room until counselor is free.

Sessions take 1 hour so the wait can be long. If more than one person waiting client is advised to go away and come back later.

In session, if the client is over 18 they get treatment protocol 1 otherwise they get treatment protocol 2.

EX 9

1007

1007

set counterA = 3

is counterA = 0 YES - take left branch

set counterB = 0

is counterB < 3 YES - take left branch

blue

increment counterB to 1

is counterB < 3 YES - take left branch

blue

increment counterB to 2

is counterB < 3 YES - take left branch

blue

increment counterB to 3

is counterB < 3 NO - take right branch

red

new line

increment counterA to 1

is counterA < 3 YES - take left branch

set counterB to 0

is counterB < 3 YES - take left branch

blue

increment counterB to 1

is counterB < 3 YES - take left branch

blue

increment counterB to 2

is counterB < 3 YES - take left branch

blue

increment counterB to 3

is counterB < 3 NO - take right branch

red

new line

increment counterA to 1

While B
D
Do A
   Do something
  E
Repeat
Until C
While B
D
Do A
   Do something
  E
Repeat
Until C
While B
D
Do A
   Do something
  E
Repeat
Until C
While B
D
Do A
   Do something
  E
Repeat
Until C
While B
D
Do A
   Do something
  E
Repeat
Until C

If A and if B then do C, otherwise do D.

HOW to FLOWCHART
"IF A and IF B"?

A is true
B is true

A is true
B is false

A is false
B is true

A is false
B is false

If A and if B then do C, otherwise do D.

A is true
B is true

A is true
B is false

A is false
B is true

A is false
B is false

1005

Backwards Diamond Branchings

Diamonds not closed

Confusing reunions of branching flows

If I have anything that is due tomorrow then if I am acing the class already and if I have some money I’ll go out drinking by myself (since all my friends will be busy), but if I don’t have any money I’ll stay home and watch reruns on cable. If, on the other hand, I’m not getting an A in this class, I’ll stay home and study. If I don’t have anything due tomorrow, then if I have some money I’ll see if some friends are around and if so I’ll party with them, otherwise, I’ll drink alone, but if I don’t have any money I’ll just stay home and watch reruns on cable.

If I have anything that is due tomorrow

if I am acing the class already

 if I have some money

I’ll go out drinking by myself (since all my friends will be busy)

 but if I don’t have any money I’ll stay home and watch reruns on cable.

If, on the other hand, I’m not getting an A in this class, I’ll stay home and study.

if I don’t have any money I’ll just stay home and watch reruns on cable.

If I don’t have anything due tomorrow, then if I have some money

I’ll see if some friends are around

 if so I’ll party with them

 I’ll drink alone

If I have anything that is due tomorrow then if I am acing the class already and if I have some money I’ll go out drinking by myself (since all my friends will be busy), but if I don’t have any money I’ll stay home and watch reruns on cable. If, on the other hand, I’m not getting an A in this class, I’ll stay home and study. If I don’t have anything due tomorrow, then if I have some money I’ll see if some friends are around and if so I’ll party with them, otherwise, I’ll drink alone, but if I don’t have any money I’ll just stay home and watch reruns on cable.

If I have anything that is due tomorrow

if I am acing the class already

 if I have some money

I’ll go out drinking by myself (since all my friends will be busy)

 but if I don’t have any money I’ll stay home and watch reruns on cable.

If, on the other hand, I’m not getting an A in this class, I’ll stay home and study.

if I don’t have any money I’ll just stay home and watch reruns on cable.

If I don’t have anything due tomorrow, then if I have some money

I’ll see if some friends are around

 if so I’ll party with them

 I’ll drink alone

If I have anything that is due tomorrow then if I am acing the class already and if I have some money I’ll go out drinking by myself (since all my friends will be busy), but if I don’t have any money I’ll stay home and watch reruns on cable. If, on the other hand, I’m not getting an A in this class, I’ll stay home and study. If I don’t have anything due tomorrow, then if I have some money I’ll see if some friends are around and if so I’ll party with them, otherwise, I’ll drink alone, but if I don’t have any money I’ll just stay home and watch reruns on cable.

anything due tomorrow?

acing the class?

have some money?

drinking by myself

stay home and watch reruns

stay home and study.

stay home and watch reruns

have some money?

friends are around?

party with them

drink alone

If I have anything that is due tomorrow then if I am acing the class already and if I have some money I’ll go out drinking by myself (since all my friends will be busy), but if I don’t have any money I’ll stay home and watch reruns on cable. If, on the other hand, I’m not getting an A in this class, I’ll stay home and study. If I don’t have anything due tomorrow, then if I have some money I’ll see if some friends are around and if so I’ll party with them, otherwise, I’ll drink alone, but if I don’t have any money I’ll just stay home and watch reruns on cable.

anything due tomorrow?

acing the class?

have some money?

drinking by myself

stay home and watch reruns

stay home and study.

stay home and watch reruns

have some money?

friends are around?

party with them

drink alone

anything due
tomorrow?

acing the class?

have some money?

drinking by myself

stay home and watch reruns

stay home and study.

stay home and watch reruns

have some money?

friends are
around?

party with them

drink alone

Modularity

modularity is the degree to which a system's components may be separated and recombined, often with the benefit of flexibility and variety in use.[1] The concept of modularity is used primarily to reduce complexity by breaking a system into varying degrees of interdependence and independence across and "hide the complexity of each part behind an abstraction and interface" WIKIPEDIA

<

LEGO klodser©2015 LEGO/Palle Peter SkovPALLE PETER SKOV/LEGO

 

infancy
childhood
adolescence

 

career
retirement

university  college  military   apprenticeship

an appetizer + a main dish + side dishes + dessert

salad

soup

 

 

 

shellfish

nuts

ice cream

pie

 

 

 

tiramisu

cake

beans

potatoes

 

 

 

peas

carrots

fish

beef

 

 

 

chicken

pasta

I kicked the ball.

We

She

John

 

 

 

They

You

Someone

caught

found

brought

 

 

 

took

painted

insulted

car

dog

wall

 

 

 

house

dice

nun

Why Modularity?

Introduction to making modular Techno

Let's Make Some Modular Art

Draw anything inside the square as long as it terminates on the edges with a dark line at the gateways.

human flow charts

Reese: get a number, pass to Chyna

Chyna: receive number, divide by 2; pass quotient to Marcin, remainder to Lucky

Marcin: if quotient is not zero, pass it to Reese; otherwise, tell Cindy to read what Lucky has written.

Lucky: write the remainder down, right to left

Bubble Sort

data[1] data[2] data[3] data[4] data[5]
7 2 9 5 4

Next Time

LOGIC

condition A

condition C

condition C

condition C

condition C

S

T

U

V

W

X

Y

Z

condition B

condition B

How to convert dec to bin?

Start with decimal number, "D"

Divide by 2. 
Note the result.
Write down the remainder.

Repeat process with the result.

Continue until nothing left.

D=6

divide by 2

result = 3, remainder = 0

divide by 2

result = 1, remainder = 1

divide by 2

result = 0, remainder = 1

610 = 1102

DEC2BIN Algorithm

Start with decimal number, "D"

Divide by 2. 
Note the result.
Write down the remainder.

Repeat process with the result.

Continue until nothing left.

call decimal number D

divide D by 2

write remainder R to L

replace D with result

result=0?

read remainders

STOP+THINK. Convert 123 to binary

 123 /2 =    61 R1
  61 /2 =    30 R1
  30 /2 =    15 R0 
  15 /2 =     7 R1
   7 /2 =     3 R1
   3 /2 =     1 R1
   1 /2 =     0 R1

1

0

0

1

1

1

1

1

1

0

1

1

1

1

1

2

0

32

64

16

8

Outtakes

Exhaustive & Mutually Exclusive

Exhaustive means the given options represent all possibilities

Mutually Exclusive means the given options do not overlap

In Management and Strategy 

Exhaustive & Everything Else

action A

action B

action C

green hair/fur/feathers?

2 legs?

action A

action A

action B

action B

action C

action C

action C

Mutually Exclusive

Flowcharts describe DETERMINISTIC processes

At any decision point the condition can be evaluated and one branch or another can be chosen.

Our convention of requiring decisions to be based on  YES-NO or TRUE FALSE conditions ensures that branches are mutually exclusive.

Exhaustive and Mutually Exclusive

How to proceed at an intersection: right or left

Secret codes: if the text contains the word "bread," launch the app; if the text contains the phrase "bread and roses," don't launch the app; if the text contains the word "roses," shut the system down.

Compute X. If X is greater than 0 and less than 12, start program A; if X is greater than 10, launch program B. If X = 11, launch program C.

What to order?  If you are hungry and thirsty, order a hot dog and a beer; if you are hungry but not thirsty, order a pretzel; if you are thirsty, order a cola.

If you are over 40 you can sign up for a vaccine in May. If you are under 30 you can sign up for a vaccine in June. If you are in an at-risk category you can sign up now.

Exhaustive and Mutually Exclusive

Ordinary Deck of Cards. Choose one card at random. Which statement are exhaustive, which are muutlly exclusive?

"you chose a red card" and "you chose a black card"

"you chose a 'face' card" and "you chose an ordinary card"

"you chose a heart," "you chose a club," and "you chose a diamond"

"your card is a number card," "your card is red", "your card is clubs,"

"your card is spades"

"you chose a red card" and "you chose a seven"

EX

ME

EX

ME

EX

ME

EX

ME

EX

ME