working with algorithm and flowcharts understanding their practicality

26
Working With Algorithm and Flowcharts Understanding their practicality

Upload: zack-wilcott

Post on 28-Mar-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Working With Algorithm and Flowcharts Understanding their practicality

Working With Algorithm and Flowcharts

Understanding their practicality

Page 2: Working With Algorithm and Flowcharts Understanding their practicality

What is an Algorithm?

It is a finite set of rules giving a sequence of operations for solving a specific type of problem.

In most common computer applications an algorithmic solution to a problem must be constructed.

Page 3: Working With Algorithm and Flowcharts Understanding their practicality

Design of an Algorithm

The design of an algorithm tends to be difficult when the task is non-trivial.

Computers tend to lack the intuition or ‘common sense’ to realise the full procedure.

The designer must describe the procedure very precisely for it to function properly

Top-down design approach is often used.

Page 4: Working With Algorithm and Flowcharts Understanding their practicality

How does the top-down method work?

First try to break the process into a number of steps, which are smaller and simpler than that for the entire process.

The sub-algorithms can themselves be broken down into smaller portions

We use flow charts to show how the algorithm will work.

Page 5: Working With Algorithm and Flowcharts Understanding their practicality

Let’s take a look at an algorithm

Image you had to program a robot to make coffee.

Page 6: Working With Algorithm and Flowcharts Understanding their practicality

A Simple Algorithm : Making CoffeeAlgorithm

1 Boil water

2 Put coffee

Page 7: Working With Algorithm and Flowcharts Understanding their practicality

A Simple Algorithm : Making CoffeeOriginal First Refinement

1 Boil water

2 Put coffee

1.1 fill kettle

1.2 switch kettle on

1.3 wait until boiling

1.4 switch off kettle

2.1 open coffee jar

2.2 Fill spoon with coffee

2.3 tip spoonful into cup

2.4 close coffee jar

Page 8: Working With Algorithm and Flowcharts Understanding their practicality

A Simple Algorithm : Making CoffeeOriginal First Refinement Second Refinement

1 Boil water

2 Put coffee

1.1 fill kettle

1.2 switch kettle on

1.3 wait until boiling

1.4 switch off kettle

2.1 open coffee jar

2.2 Fill spoon with coffee

2.3 tip spoonful into cup

2.4 close coffee jar

1.1.1 put kettle under

tap

1.1.2 turn on tap

1.1.3 wait till kettle is full

1.1.4 turn off tap

2.1.1 take coffee jar from shelf

2.1.2 remove lid

2.4.1 put lid on coffee jar

2.4.2 replace coffee jar on shelf

Page 9: Working With Algorithm and Flowcharts Understanding their practicality

Selection (Decision Statement)

An algorithm which is solely a sequence of steps is extremely inflexible

A ‘What if …? Then’ Statement allows for this flexibility.

What if the coffee jar is empty?

Then get a new jar.

Page 10: Working With Algorithm and Flowcharts Understanding their practicality

Iteration (Repetition or Loop)

Sometimes it is necessary for the computer not just to make a decision, but a continuous action.

This processes is called iteration.

Example : With the robot making the coffee

Continue filling coffee jar until jar is found.

Page 11: Working With Algorithm and Flowcharts Understanding their practicality

What Is a Flowchart?

A diagrammatic representation of a program

Building a flowchart is like putting a puzzle together; there are standard pieces

The standard action shapes include terminal, process, in/output, decision and junction.

Page 12: Working With Algorithm and Flowcharts Understanding their practicality

What Are the Basic Symbols?

A program should always begin with a start symbol and finish symbol

For Pascal it equals the begin and the end

Start

End

Page 13: Working With Algorithm and Flowcharts Understanding their practicality

What Are the Basic Symbols?

Whenever a decision must be taken a diamond symbol is used.

Can you see the correlation to the if…then…else… statements.

no

Is mark

> 50?

yes

Decision/ Comparison Operation

Page 14: Working With Algorithm and Flowcharts Understanding their practicality

What Are the Basic Symbols?

Used to connect different parts of the program together.

Connector

Page 15: Working With Algorithm and Flowcharts Understanding their practicality

What Are the Basic Symbols?

A rectangle represents a process.

When a variable must be assigned to a value, a process must be used

When clearing the screen, changing colour a rectangle must be used

mark := 0

Process to be performed

Page 16: Working With Algorithm and Flowcharts Understanding their practicality

What Are the Basic Symbols?

Input and Output are represented using a parallelogram.

In Pascal we are dealing with writeln, write, or readln.Show result

to user

Read mark from user

Input /Output

Operation

Page 17: Working With Algorithm and Flowcharts Understanding their practicality

Let’s Put the Pieces Together!

Example

You are going to school, how will you get there?

Page 18: Working With Algorithm and Flowcharts Understanding their practicality

Leave the House

Page 19: Working With Algorithm and Flowcharts Understanding their practicality

Leave the House

Check the Weather

Page 20: Working With Algorithm and Flowcharts Understanding their practicality

Leave the House

Check the Weather

Weather Clear?

Page 21: Working With Algorithm and Flowcharts Understanding their practicality

Leave the House

Check the Weather

Weather Clear?

Yes

Walk

Page 22: Working With Algorithm and Flowcharts Understanding their practicality

Leave the House

Check the Weather

Weather Clear?

Yes

Walk

Arrive at School

Page 23: Working With Algorithm and Flowcharts Understanding their practicality

Leave the House

Check the Weather

Weather Clear?

Yes

Walk

Arrive at School

No, Rain.

Page 24: Working With Algorithm and Flowcharts Understanding their practicality

Leave the House

Check the Weather

Weather Clear?

Yes

Walk

Arrive at School

No, Rain.

Put on raincoat

Page 25: Working With Algorithm and Flowcharts Understanding their practicality

Leave the House

Check the Weather

Weather Clear?

Yes

Walk

Arrive at School

No, Rain.

Put on raincoat

Ride Bus

Page 26: Working With Algorithm and Flowcharts Understanding their practicality

Leave the House

Check the Weather

Weather Clear?

Yes

Walk

Arrive at School

No, Rain.

Put on raincoat

Ride Bus