algorithms, algorithmic thinking and structured code

Post on 28-Dec-2015

221 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Algorithms,

Algorithmic Thinking and

Structured Code

Tell me how to make a Jam Sandwich

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

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

Origins of Algorithms

Edsger Wybe Dijkstra

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

“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

“Go to the other side of the road”

“Go to Hell”

Meaningful Instructions

Effective

Meaningful Instructions

Unambiguous

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

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

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

“peel the potatoes”

Halting

“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

“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

“peel the potatoes”

Instruction Blocks

WHILE condition is true DO

action1

action2

action3

END WHILE

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

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

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

Computational Thinking - 1

Computational Thinking – 2

(Eight Coins Puzzle)

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

Music

Algorithmic?

get the potato basket from the cellar,

get the pan from the cupboard,

peel the potatoes

return the basket to the cellar

I

Have

A

Lovely

Bunch

Of

Coconuts

x = 5

Y = 3

Temp = X

X = Y

Y = Temp

Computational Thinking – 3

Eight Coins Puzzle

top related