Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
243 views

Components of An Algorithm

The document describes the key components of an algorithm: values and variables, instructions (also called primitives), sequence, procedure, selection, and repetition. It provides examples and explanations of each component. Instructions are the basic building blocks that perform simple unambiguous actions. Sequence structures a series of instructions. Procedures allow grouping instructions under a name. Selection structures conditional execution based on a true/false test.

Uploaded by

abhimanyu raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
243 views

Components of An Algorithm

The document describes the key components of an algorithm: values and variables, instructions (also called primitives), sequence, procedure, selection, and repetition. It provides examples and explanations of each component. Instructions are the basic building blocks that perform simple unambiguous actions. Sequence structures a series of instructions. Procedures allow grouping instructions under a name. Selection structures conditional execution based on a true/false test.

Uploaded by

abhimanyu raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Components of an Algorithm

1
Components of an Algorithm
Values and Variables
• Instruction (a.k.a. primitive)
– Sequence (of instructions)
– Procedure (involving instructions)
– Selection (between instructions)
– Repetition (of instructions)
• Documentation (beside instructions)

2
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
– Sequence (of instructions)
– Procedure (involving instructions)
– Selection (between instructions)
– Repetition (of instructions)
• Documentation (beside instructions)

3
Instructions (Primitives)
• Some action that is
– simple
– unambiguous
– that the system knows about...
– ...and should be able to actually do

4
Instructions – Examples
• Take off your shoes Directions to perform
• Count to 10 specific actions on values
• Cut along dotted line and variables.
• Pull rip-cord firmly
• Shift 10 grams of arsenic
• Run for 2 kms

5
Instructions -- Application
• Some instructions can only be applied to a
specific type of values or variables
• Examples:

6
Instructions (Primitives) --
Recommendations
• When writing an algorithm, make each
instruction simple and unambiguous
• Example:

Cut chicken into pieces and Cut chicken into pieces.


brown the pieces on all Heat olive oil in a casserole
sides in a casserole dish dish.
in hot olive oil. Brown the chicken pieces in
the casserole dish. 7
Instruction (Primitives)
• When A writing an algorithm,
“sequence” of make the
instructions
simplesimple and unambiguous.
instructions
• Example:

Cut chicken into pieces and Cut chicken into pieces.


brown the pieces on all Heat olive oil in a casserole
sides in a casserole dish in
dish.
hot olive oil.
Brown the chicken pieces in
the casserole dish. 8
Sequence

• A series of instructions
• ...to be carried out one after the other...
• ...without hesitation or question
• Example:
– How to make tea

9
Sequence -- Example
1. Switch on gas
2. Put pan on burner
3. Pore water into the pan
4. Add tea leaves into the water
5. Boil the mix
6. Add milk
7. Add sugar
8. Boil the mix again
9. Switch off the gas
10. Pore tea into cups
11. Serve the cup of tea
10
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
– Procedure (involving instructions)
– Selection (between instructions)
– Repetition (of instructions)
• Documentation (beside instructions)

11
Procedure
• A named sequence of instructions
• So that you can
– Refer to it collectively (by name)
– ...instead of individually (by each instruction in
the sequence)
• Example:
– Drive_To_Uni

12
Procedure -- Example
procedure Drive_To_Uni
{
1. find car keys ...etc...etc...etc...
2. open car door 52. find parking space
3. get in car 53. pull into parking
4. shut car door space
5. put keys in ignition 54. turn off engine
6. start car 55. remove keys from
7. back car out of ignition
driveway 56. open car door
8. drive to end of street
57. get out
9. turn right
58. shut car door
10. drive to end of street
11. turn left 59. lock car door
...etc...etc...etc }
13
Procedure – Example (cont)
procedure Do_Wednesday procedure Do_Week
{ {
Wake_up Do_Monday
Have_Shower
Do_Tuesday
Eat_Breakfast
Do_Wednesday
Drive_To_Uni
Sit_C_Prog_Lecture Do_Thursday
...etc...etc...etc... ...etc...etc...etc...
Drive_From_Uni }
...etc...etc...etc...
}
14
Procedure – Example (cont)
procedure Do_Wednesday In this subject, we also
{ use the following words
Wake_up to refer to a
Have_Shower
“Procedure” :
Eat_Breakfast
Drive_To_Uni • Sub-routine
Sit_1301_Lecture • Method
...etc...etc...etc... • Function
Drive_From_Uni
• Module
...etc...etc...etc...
}
15
Procedure – Example (cont)
procedure Do_Wednesday
{
Wake_up We use brackets to
Have_Shower mark the beginning
Eat_Breakfast
and end of a
Drive_To_Uni
sequence.
Sit_1301_Lecture
...etc...etc...etc...
Drive_From_Uni
...etc...etc...etc...
}
16
Procedure – Example (cont)
procedure Do_Wednesday
{
Wake_up
Have_Shower
Eat_Breakfast
An instruction invoking
Drive_To_Uni a procedure is known
Sit_1301_Lecture as a “procedure call”
...etc...etc...etc...
Drive_From_Uni
...etc...etc...etc...
}
17
Procedure
• A procedure may have a set of parameters
procedure customerService ( myName ,timeOfDay )
{
say “Good timeOfDay”
say “My name is myName”
say “How can I help you?”
}

customerService ( “Ann”, “Morning” )


customerService (“Ann”, “Afternoon” )
customerService ( “Jeff”, “Afternoon” )
18
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
– Selection (between instructions)
– Repetition (of instructions)
• Documentation (beside instructions)

19
Selection
• An instruction that decides which of two
possible sequences is executed
• The decision is based on a single true/false
condition
• Examples:
– Car repair
– Reciprocals

20
Selection Example -- Car Repair
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburettor
}
else
{
CheckDistributor
CheckIgnitionCoil
}
21
Selection Example –
Car Repair (cont)
if (motor turns)
then
{ Should be a
CheckFuel true or false
CheckSparkPlugs condition.
CheckCarburettor
}
else
{
CheckDistributor
CheckIgnitionCoil
} 22
Selection Example --
Car Repair (cont)
if (motor turns)
then
{
CheckFuel
Sequence if
CheckSparkPlugs the condition
CheckCarburettor is true.
}
else
{
CheckDistributor
CheckIgnitionCoil
} 23
Selection Example --
Car Repair (cont)
if (motor turns)
then
{
CheckFuel
CheckSparkPlugs
CheckCarburettor Sequence if the
} condition is
else
false.
{
CheckDistributor
CheckIgnitionCoil
} 24
Selection Example -- Reciprocals

Examples:
Q. Give an
algorithm for Reciprocal of 2: 1/2
computing the
Reciprocal of -3/4:
reciprocal of a
1/(-3/4) = -4/3
number.
Reciprocal of 0:
“undefined”

25
Selection Example – Reciprocals (cont)
Algorithm:
input Num
if (Num is not equal 0)
Q. Give an then
algorithm for {
computing the output 1/Num
reciprocal of a }
number. else
{
output "infinity"
}
26
Selection Example-- Reciprocals
Algorithm:
input Num
Num is a variable if (Num is not equal 0)
whose value then
depends on the {
actual number the output 1/Num
user provides. }
else
{
output "infinity"
}
27
Selection Example – Reciprocals
(cont)
Algorithm:
input Num
if (Num is not equal 0)
Condition depends then
on the value of {
Num output 1/Num
}
else
{
output "infinity"
}
28
Selection Example – Reciprocals
(cont)
Algorithm:
input Num
if (Num is not equal 0)
For a given value then
{
of Num, only one
output 1/Num
of these two }
sequences can be else
executed {
output "infinity"
}
29
Selection Example – Reciprocals
(cont)
Algorithm:
input Num
if (Num is not equal 0)
then
{
Executed if Num output 1/Num
is not equal to 0 }
else
{
output "infinity"
}
30
Selection Example – Reciprocals
(cont)
Algorithm:
input Num
if (Num is not equal 0)
then
{
output 1/Num
}
Executed if Num else
{
is equal to 0
output "infinity"
} 31
Selection -- Exercise
Will the following algorithms produce the same output?

Algorithm 1: Algorithm 2:
input Num input Num
if (Num is not equal 0) if (Num is not equal 0)
then then
{ {
output 1/Num
output 1/Num
}
}
else
{
output "infinity" output "infinity"
} 32
Selection – Several Conditions
• What if several conditions need to be satisfied?
if ( today is Wednesday and the time is 10.00am )
then
{
Go to Lecture
}
else
{
Go to Library
} Solution 1
33
Selection – Several Conditions (cont)
if ( today is Wednesday )
then
{
if ( the time is 10.00am )
then
{
Go to Lecture
Often called a
} “nested selection”
}
else
...etc...etc...etc...
Solution 2

34
Selection – At Least One of
Several Conditions
• What if at least one of several conditions
needs to be satisfied?
if ( I feel hungry or the time is 1.00pm or my
mate has his eye on my lunch )
then
{
Eat my lunch now
}

35
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
• Repetition (of instructions)
• Documentation (beside instructions)
36
Repetition
• Repeat an instruction...
– ...while (or maybe until) some true or
false condition occurs
– Test the condition each time before
repeating the instruction
• Also known as iteration or loop
• Example:
– Algorithm for getting an appointment
37
Repetition -- Example
procedure AskOnDate ( name, time )
{
Phone(name)
Say("Hey I am", name)
Say("Wanna meet at", time, "?")
ListenToReply ( )
start waiting count at zero
while ( reply is "No" and waiting count < 100)
{
Say("Oh its ok!")
add 1 to begging count
ListenToReply ( )
}
}
38
Repetition – Example (cont)
procedure AskOnDate ( name, time )
{
Phone(name)
Say("Hey I am", name)
Say("Wanna meet at", time, "?")
ListenToReply ( )
start waiting count at zero
while ( reply is "No" and waiting count < 100 )
{
Say("Oh its ok!") Condition
is tested
add 1 to begging count
ListenToReply ( )
before sequence
}
}
39
Repetition – Example (cont)
procedure AskOnDate ( name, time )
{
Phone(name)
Say("Hey I am", name)
Say("Wanna meet at", time, "?")
ListenToReply ( )
start waiting count at zero
while ( reply is "No" and waiting count < 100)
{ Sequence
may not
Say("Oh its ok!")
add 1 to begging count get executed at all
ListenToReply ( )
}
} 40
Repetition – Example (cont)
Ensure initial
procedure AskOnDate ( name, time )
{
values of variables
Phone(name) used in the
Say("Hey I am", name) conditions are set
Say("Wanna meet at", time, "?")
ListenToReply ( )
correctly
start waiting count at zero
while (reply is "No" and waiting count < 100)
{
Say("Oh its ok!")
add 1 to begging count
ListenToReply ( )
}
}
41
Repetition – Example (cont)
procedure AskOnDate ( name, time )
{
Phone(name)
Say("Hey I am", name)
Say("Wanna meet at", time, "?")
ListenToReply ( )
start waiting count at zero
while (reply is "No" and waiting count < 100)
{
Say("Oh its ok!") Ensure the variables
add 1 to begging count used in the conditions
ListenToReply ( ) are updated in each
}
} iteration 42
Repetition – Example (cont)
• What if we don’t increment the begging count?
procedure AskOnDate ( name, time )
{
Phone(name)
Say("Hey I am", name)
Say("Wanna meet at", time, "?")
ListenToReply ( )
start waiting count at zero
while (reply is "No" and waiting count < 100)
{
Say("Oh its ok!")
}
}
Infinite loop
43
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
Repetition (of instructions)
• Documentation (beside instructions)
44
Documentation
• Records what the algorithm does
• Describes how it does it
• Explains the purpose of each component of
the algorithm
• Notes restrictions or expectations
• Example:
– Getting a date (again)

45
Documentation -- Example
Think of meeting

Try to book an appointment


initialise booking to “unsuccessful”
until (successfully booked)
{
get next time
AskOnDate(Name, Time)
DetermineBookingSuccess
}

46
Components of an Algorithm
Values and Variables
Instruction (a.k.a. primitives)
Sequence (of instructions)
Procedure (involving instructions)
Selection (between instructions)
Repetition (of instructions)
Documentation (beside instructions)
47
The Software Development Process

• Define the problem clearly


• Analyse the problem thoroughly
• Design an algorithm carefully
• Code the algorithm efficiently
• Test the code thoroughly
• Document the system lucidly

48
Top-down Algorithm Design
• Write down what you have to do
• Break that into 3-7 smaller steps
• Break each step into 3-7 smaller steps
• Keeping subdividing until each individual
step is easy enough to do – i.e., until it is a
single instruction
• Example:
– Learning
49
Top-down Design -- Example
Read lecture notes
Read
Read textbook
Make notes
Prepare Prepare questions
Read exercise
Attend lecture
Design algorithm
Listen and think
Learn Study Complete prac
Code solution
Test and document
Attend tute
Record insights
Reinforce Record answers to
questions
Revise notes

50

You might also like