Sunteți pe pagina 1din 26

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.

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.

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.

Lets take a look at an algorithm

Image you had to program a robot to make coffee.

A Simple Algorithm : Making Coffee


Algorithm

1 Boil water

2 Put coffee

A Simple Algorithm : Making Coffee


Original First Refinement

1 Boil water

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

2 Put coffee

A Simple Algorithm : Making Coffee


Original First Refinement Second Refinement

1 Boil water

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

2 Put coffee

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

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.

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.

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.

What Are the Basic Symbols?

Start

A program should always begin with a start symbol and finish symbol For Pascal it equals the begin and the end

End

What Are the Basic Symbols?

yes

Whenever a decision must be taken a diamond symbol is used. Can you see the correlation to the ifthenelse statements.

Is mark
> 50?

no

Decision/ Comparison Operation

What Are the Basic Symbols?

Used to connect different parts of the program together.

Connector

What Are the Basic Symbols?


mark := 0

Process to be performed

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

What Are the Basic Symbols?

Read mark from user

Input and Output are represented using a parallelogram. In Pascal we are dealing with writeln, write, or readln.

Show result to user Input /Output Operation

Lets Put the Pieces Together!

Example

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

Leave the House

Leave the House

Check the Weather

Leave the House

Check the Weather

Weather Clear?

Leave the House

Check the Weather

Weather Clear?
Yes Walk

Leave the House

Check the Weather

Weather Clear?
Yes Walk

Arrive at School

Leave the House

Check the Weather


No, Rain.

Weather Clear?
Yes Walk

Arrive at School

Leave the House

Check the Weather


No, Rain.

Put on raincoat

Weather Clear?
Yes Walk

Arrive at School

Leave the House

Check the Weather


No, Rain.

Put on raincoat

Weather Clear?
Yes Walk

Ride Bus

Arrive at School

Leave the House

Check the Weather


No, Rain.

Put on raincoat

Weather Clear?
Yes Walk

Ride Bus

Arrive at School

S-ar putea să vă placă și