algorithms, algorithmic thinking and structured code

56
Algorithms, Algorithmic Thinking and Structured Code

Upload: tiffany-day

Post on 28-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms, Algorithmic Thinking and Structured Code

Algorithms,

Algorithmic Thinking and

Structured Code

Page 2: Algorithms, Algorithmic Thinking and Structured Code
Page 3: Algorithms, Algorithmic Thinking and Structured Code

Tell me how to make a Jam Sandwich

Page 4: Algorithms, Algorithmic Thinking and Structured Code
Page 5: Algorithms, Algorithmic Thinking and Structured Code

Tell me how to draw this tree (write the instructions in Simple English)

Page 6: Algorithms, Algorithmic Thinking and Structured Code
Page 7: Algorithms, Algorithmic Thinking and Structured Code

Tell me how to count the money in my right pocket (write the instructions in Simple English)

Page 8: Algorithms, Algorithmic Thinking and Structured Code
Page 9: Algorithms, Algorithmic Thinking and Structured Code

Origins of Algorithms

Edsger Wybe Dijkstra

Page 10: Algorithms, Algorithmic Thinking and Structured Code
Page 11: Algorithms, Algorithmic Thinking and Structured Code

Spaghetti Code mov eax,3

mov ebx,3

 L1: decjmpreg eax,L2

decjmpreg ebx,L3

jmp L1

L2: eax = 0

decjmpreg ebx, L4

jmp L5

L3: ebx = 0

inc eax

decjmpreg eax, L4

jmp L5

 L4: mov eax,0

hlt

L5: mov eax,1

hlt

Page 12: Algorithms, Algorithmic Thinking and Structured Code
Page 13: Algorithms, Algorithmic Thinking and Structured Code

“Peeling Potatoes for a Family Supper”

We observe the cook doing this

“He gets the potato basket from the cellar,

gets the pan from the cupboard,

peels the potatoes

returns the basket to the cellar”

Sequence

Page 14: Algorithms, Algorithmic Thinking and Structured Code
Page 15: Algorithms, Algorithmic Thinking and Structured Code

“Go to the other side of the road”

“Go to Hell”

Meaningful Instructions

Effective

Page 16: Algorithms, Algorithmic Thinking and Structured Code
Page 17: Algorithms, Algorithmic Thinking and Structured Code

Meaningful Instructions

Unambiguous

A wedding journalist reported that the four bridesmaids wore the same dress.

Page 18: Algorithms, Algorithmic Thinking and Structured Code
Page 19: Algorithms, Algorithmic Thinking and Structured Code

Instructions

“He gets the potato basket from the cellar,

gets the pan from the cupboard,

peels the potatoes

returns the basket to the cellar”

get the potato basket from the cellar,

get the pan from the cupboard,

peel the potatoes

return the basket to the cellar

Sequence: Correct Order

Page 20: Algorithms, Algorithmic Thinking and Structured Code
Page 21: Algorithms, Algorithmic Thinking and Structured Code

Jordan does something different

get the potato basket from the cellar,

get the pan from the cupboard,

IF clothes are light colored THEN put on an apron

peel the potatoes

return the basket to the cellar

IF some condition THEN some action

Selection

Page 22: Algorithms, Algorithmic Thinking and Structured Code
Page 23: Algorithms, Algorithmic Thinking and Structured Code

“peel the potatoes”

Halting

Page 24: Algorithms, Algorithmic Thinking and Structured Code
Page 25: Algorithms, Algorithmic Thinking and Structured Code
Page 26: Algorithms, Algorithmic Thinking and Structured Code
Page 27: Algorithms, Algorithmic Thinking and Structured Code

“peel the potatoes”

IF number of peeled potatoes is not sufficient THEN peel a potato

IF number of peeled potatoes is not sufficient THEN peel a potato

IF number of peeled potatoes is not sufficient THEN peel a potato

IF number of peeled potatoes is not sufficient THEN peel a potato

IF number of peeled potatoes is not sufficient THEN peel a potato

Page 28: Algorithms, Algorithmic Thinking and Structured Code
Page 29: Algorithms, Algorithmic Thinking and Structured Code

“peel the potatoes”

WHILE the number of potatoes is insufficient DO peel a potato

get the potato basket from the cellar,

get the pan from the cupboard,

IF clothes are light colored THEN put on an apron

WHILE the number of potatoes is insufficient DO peel a potato

return the basket to the cellar

WHILE condition is true DO an action

Iteration

Page 30: Algorithms, Algorithmic Thinking and Structured Code
Page 31: Algorithms, Algorithmic Thinking and Structured Code

“peel the potatoes”

Instruction Blocks

WHILE condition is true DO

action1

action2

action3

END WHILE

Page 32: Algorithms, Algorithmic Thinking and Structured Code
Page 33: Algorithms, Algorithmic Thinking and Structured Code

Algorithms – a definition

• Sequence of Instructions (ordered)

• Instructions are Effective and Unambigous

• Sequence must Halt in a finite time

• On halting, the sequence must have a Result

• Instructions may be

• Simple Actions

• Selections

• Iterations

action

IF condition THEN action

WHILE condition is true DO action

Page 34: Algorithms, Algorithmic Thinking and Structured Code
Page 35: Algorithms, Algorithmic Thinking and Structured Code

Pseudo-code for Structured Programming

WHILE condition is true

action

action

END WHILE

DO

action

action

WHILE condition is true

IF condition is true

action

action

END IF

actions

move robot forward by 20 cm

get a coin from your pocket

press a key on your phone

conditions

robot sensor shows obstacle

coin is a 50p

pocket is empty

key pressed is 7

compound conditions

NOT condition1

condition1 AND condition2

condition1 OR condition2

actions

add, subtract, multiply, divide

get, put (set)

conditions

equals

less than, greater than

combi’s of above

Page 36: Algorithms, Algorithmic Thinking and Structured Code
Page 37: Algorithms, Algorithmic Thinking and Structured Code

Pseudo-code Examples

1. Robot moves forward until senses obstacle

2. Check that you have typed in a password that ends with 5

3. Check that you have entered a 6-digit phone number

Page 38: Algorithms, Algorithmic Thinking and Structured Code
Page 39: Algorithms, Algorithmic Thinking and Structured Code

Computational Thinking - 1

Page 40: Algorithms, Algorithmic Thinking and Structured Code
Page 41: Algorithms, Algorithmic Thinking and Structured Code

Computational Thinking – 2

(Eight Coins Puzzle)

Page 42: Algorithms, Algorithmic Thinking and Structured Code
Page 43: Algorithms, Algorithmic Thinking and Structured Code
Page 44: Algorithms, Algorithmic Thinking and Structured Code
Page 45: Algorithms, Algorithmic Thinking and Structured Code

Dr.C’s “Atomic Language” Conjecture(weak version)

Any computer program can be expressed using only the following:

•Declaration: as many variable you need

•Just 2 Operations:

•One loop construct:

We do not need:

IF condition THEN action

FOR loop

Operations of multiplication, division and anything else.

WHILE condition action actionEND WHILE

add1 to a variablesubtract 1 from a variable

Page 46: Algorithms, Algorithmic Thinking and Structured Code
Page 47: Algorithms, Algorithmic Thinking and Structured Code

Music

Algorithmic?

Page 48: Algorithms, Algorithmic Thinking and Structured Code
Page 49: Algorithms, Algorithmic Thinking and Structured Code

get the potato basket from the cellar,

get the pan from the cupboard,

peel the potatoes

return the basket to the cellar

Page 50: Algorithms, Algorithmic Thinking and Structured Code
Page 51: Algorithms, Algorithmic Thinking and Structured Code

I

Have

A

Lovely

Bunch

Of

Coconuts

Page 52: Algorithms, Algorithmic Thinking and Structured Code
Page 53: Algorithms, Algorithmic Thinking and Structured Code

x = 5

Y = 3

Temp = X

X = Y

Y = Temp

Page 54: Algorithms, Algorithmic Thinking and Structured Code
Page 55: Algorithms, Algorithmic Thinking and Structured Code

Computational Thinking – 3

Eight Coins Puzzle

Page 56: Algorithms, Algorithmic Thinking and Structured Code