Documente Academic
Documente Profesional
Documente Cultură
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.
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.
1 Boil water
2 Put coffee
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 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
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.
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?
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.
Start
A program should always begin with a start symbol and finish symbol For Pascal it equals the begin and the end
End
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
Connector
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
Input and Output are represented using a parallelogram. In Pascal we are dealing with writeln, write, or readln.
Example
Weather Clear?
Weather Clear?
Yes Walk
Weather Clear?
Yes Walk
Arrive at School
Weather Clear?
Yes Walk
Arrive at School
Put on raincoat
Weather Clear?
Yes Walk
Arrive at School
Put on raincoat
Weather Clear?
Yes Walk
Ride Bus
Arrive at School
Put on raincoat
Weather Clear?
Yes Walk
Ride Bus
Arrive at School