flowcharts, modularity, and numbers
inf1339
faculty of information
university of toronto
Outcomes
- Pseudocode2Flowchart
- Flowchart2Pseudocode
- Real World ↔ Pseudocode ↔ Flowchart
- Critique and Fix Ill-formed flowcharts
- Flowcharts and stepwise refinement
- Flowcharts and black boxes
- Flowcharts and modularity
- Binary numbers
- count by 2, powers of 2, bin2dec, dec2bin
- Hexadecimal numbers
- count by x, bin2hex, hex2bin, hex2dec, dec2hex
Review Questions
Tutorial
Problem Sets

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 both IN FROM and OUT TO THE SIDES of a connector circle.
\
O
\
O
\
O
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
New Concept: Flowchart for Repetition
WHILE A DO B
A?
B
YES
NO
SINGLE EXIT POINT
SINGLE ENTRY POINT
"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?
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
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.

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
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
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
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

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

What's Wrong with this Survey Question?
Q2. What is your gender?
Female
Male
Q2. What is your gender?
Female
Male
What's Wrong with this Survey Question?
Q3. Which category are you in?
Canadian
Student
Climate Change Denier
Big Fan of Teddy Bears
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
STRETCH
NUMBERS
NUMBERS
LOGIC
condition A
condition C
condition C
condition C
condition C
S
T
U
V
W
X
Y
Z
condition B
condition B
What You Need to Know
-
CL01 Binary: Concept of base 2; notation; count to 10; dec2bin, bin2dec; first 10 powers of 2; binary arithmetic
-
CL02 Hexadecimal: Concept of base 16; notation; hex2bin, bin2hex, hex2dec; ASCII encoding; URL encoding; HTML color encoding
-
CL03 Counting by Powers: exponents, graphing, logarithms



Binary = Base 2
Positional Number Systems

Positional Number Systems

Positional Number Systems

STOP+THINK
Count to ten in binary
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010
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
STOP+THINK. Convert 273 to binary
STOP+THINK. Convert 2421 to binary
STOP+THINK. Convert 7777 to binary
7777 / 2 = 3888 R1
3888 / 2 = 1944 R0
1944 / 2 = 972 R0
972 / 2 = 486 R0
486 / 2 = 243 R0
243 /2 = 121 R1
121 /2 = 60 R1
60 /2 = 30 R0
30 /2 = 15 R0
15 /2 = 7 R1
7 /2 = 3 R1
3 /2 = 1 R1
1 /2 = 0 R1
1
0
0
0
0
1
1
0
0
1
1
1
1
7777 / 2 = 3888 R1
3888 / 2 = 1944 R0
1944 / 2 = 972 R0
972 / 2 = 486 R0
486 / 2 = 243 R0
243 /2 = 121 R1
121 /2 = 60 R1
60 /2 = 30 R0
30 /2 = 15 R0
15 /2 = 7 R1
7 /2 = 3 R1
3 /2 = 1 R1
1 /2 = 0 R1
1
0
0
0
0
1
1
0
0
1
1
1
1
4096 + 2048 + 1024 + 512 + 64 + 32 + 1
An earlier question asked how we might count if we had 8 fingers, four on each hand, and we came up with the idea that we would only need the digits 0, 1, 2, 3, 4, 5, 6, and 7.
STOP+THINK: But what if we had 8 fingers on EACH hand? How would we count?
When we were counting by something LESS than ten, we simply left some of the usual digits out of the picture. If we lived in a base-8 world (where we had 8 fingers) the digits 8 and 9 would just be silly and unnecessary symbols. If we lived in that world and wanted to think about the imaginary world in which we had ten fingers and so would count by ten we'd need two extra symbols after 7. We could pick any at all. Maybe 0,1,2,3,4,5,6,7,X,Q,10.
So, what if we were counting by sixteen? Then we'd need another 6 symbols and we'd count like this
1, 2, 3, 4, 5, 6, 7, 8, 9, SYMBOL1, SYMBOL2, SYMBOL3, SYMBOL4, SYMBOL5, SYMBAL6, 10
What if we decided to use the first six letters as the symbols? Then we'd have
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | 10 |
1010 | 1110 | 1210 | 1310 | 1410 | 1510 | 1610 |
And this is precisely how we do it in the base 16 or "HEXADECIMAL" number system. It turns out that hexadecimal is particularly useful as a counterpart to binary. For many reasons, but one we'll start with is that the number of values that can be represented with four binary digits is sixteen (think about it: 00002 = 010, 11112 = 1510). So, one shorthand for binary is to represent every four binary digits with a single hexadecimal digit.
Try the examples below. Match each binary number with its hexadecimal equivalent.
See Also (optional)
- The Organic Chemistry Tutor. 2018. "How To Convert Binary to Hexadecimal (Links to an external site.)" (10m13s)
- The Organic Chemistry Tutor. 2018. "How To Convert Hexadecimal to Binary (Links to an external site.)" (11m37s)
Other Bases
What if...
1
2
3
4

5
6
0, 1, 2, 3, 4, 5, ?
STOP+THINK. What's next?
0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15, 20
STOP+THINK. What's next?
Base 6
64 63 62 61 60
1296 216 36 6 1
3246 = 3*36 + 2*6 + 4*1 = 108 + 12 + 1 = 12110
What if...

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12...
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13...
STOP+THINK. The rest of binary 0 - 15
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
1010 = 10
1011 = 11
1100 = 12
1101 = 13
1110 = 114
1111 = 15
BINARY & HEXADECIMAL
24 23 22 21 20
16 8 4 2 1
164 163 162 161 160
65536 4096 256 16 1
BINARY & HEXADECIMAL
Binary Digits: 0,1
Hexadecimal Digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
HEX2DEC
3 x 163 + 10 x 162 + 15 x 161 + 7 x 160
3 x 4096 + 10 x 256 + 15 x 16 + 7 x 1
12288 + 2560 + 240 + 7
15095
3
A
F
7
STOP+THINK. HEX2DEC
FF
111
2000
ACE
C0DE
1000
=15*16+15=25510
=256+16+1=27310
=2*4096=819210
=10*256+12*16+14 =2560+206=276610
=12*4096+13*16+14
=4937410
=409610
162=256 163=4096
Click for solutions
BIN2HEX
1011010101010111101010101010
1011 0101 0101 0111 1010 1010 1010
B 5 5 7 A A A
{
HEX2BIN
1011010101010111101010101010
1011 0101 0101 0111 1010 1010 1010
B 5 5 7 A A A
{
ENCODING
ASCII
American Standard Code for Information Interchange
- Numerical code for letters, digits, symbols
- Goes back to the days of teletype, hence the
control character symbols such as BEL
- Seven binary digits encode 128 symbols



STOP+THINK. Decode this ASCII text
4465636F 64652074 68697320 41534349 49207465 787460

Other Notations
subscripts: 15916 159hex 159h
prefix: 0x1F9
special characters in URLs as hex pairs prefixed with %
characters in XML and HTML as hex pairs with ode;
colors in HTML & CSS: 3 hex pairs* (red, green and blue) prefixed with #
http://www.example.com/name%20with%20spaces
’ = character U+2019 (’).
This Orange = #FFAA33
*CSS allows 3-hex digit abbreviation, one per component: #FA3 = #FFAA33.
STOP+THINK: CSS Colors

#17e71e

#abcdef
#8800FF

Which color is
#17e71e
#abcdef
#8800FF

STOP+THINK: HTML &codes
Text
N.B. 16110 = 0xA1
Wait, there's more...

BINARY ARITHMETIC
- ADDING SINGLE BITS TOGETHER
- THE CARRY BIT
- MULTIPLYING IN BINARY
Binary
Arithmetic
1
+1
10
Arithmetic in Binary
0 + 0 = 0
0 + 1 = 1
1 + 1 = 10
10 + 1 = 11
10 + 10 = 100
STOP+THINK
Could we write
numbers
with TimBits?


STOP+THINK
Could we do math
with TimBits?

Next: LOGIC
1
+0
01
0
+1
01
0
+0
00
1
+1
10
A
+B
WX
A B W X
1 1 1 0
1 0 0 1
0 1 0 1
0 0 0 0
Flow, Modularity, and Numbers
By Dan Ryan
Flow, Modularity, and Numbers
- 724