1.1.
1 UNDERTSANDING ALGORITHMS
Algorithms
- An algorithm is a detailed design for a solution
- To say it is an algorithm there are certain
characteristics
- It should be ambiguous
*means it can’t be misunderstood(Eg – turn right )
- It is a sequence of steps
- Get the same result every time it is being run
- provides the solution to the given problem
- To decide whether it is successful it must
- It should have the expected outcome – ACCURACY
- Must produce the same result every time it is run –
CONSISTENCY
- It must be given in the shortest time possible –
Efficiency
For an instance , imagine google maps. If it
took 5 minutes to choose an map and the app
takes the same, people will not use maps while
they will use the Atlas more
Rashmitha Jayawardhana Computer science 9/10 LESSON NOTES
The difference between a program and an algorithm
• A program is where an algorithm is implemented
while an algorithm is a detailed design for an
problem
• Algorithms divide into 2 parts
1) Low level programming language
• It is a programming language that can only be
understood by computers
Eg – binary
2) high level programming
• It is a programming language that is similar
to human language and computers cannot
understand it .
Eg – python
- An algorithm can be represented in 3 ways
Written
Algorithm
Description
Flowchart
- Keep in mind that programming languages are
another way as well . for your level it is python.
Rashmitha Jayawardhana Computer science 9/10 LESSON NOTES
1.1.2 Creating algorithms
1.1.2.1Written descriptions
- It is the simplest way of expressing an algorithm.
- It is written in simple English and doesn’t have any
keywords to follow
For an instance
TO MAKE COFFEE
Fill kettle with water
Turn kettle on
Add coffee powder to cup
Wait for water to boil
Pour water to the cup
Add milk and sugar
Stir and enjoy!
Retentional operators and arithmetic operators
They are the values and terms that should be used in
ANY algorithm when doing mathematical functions
Rashmitha Jayawardhana Computer science 9/10 LESSON NOTES
Arithmetic operators
Operators Functions Example
+ Add values Num1+num2
- Subtract Num1-Num2
values
* Multiply Num1*Num2
values
/ Divide values Num1/Num2
DIV(quotient ) Divides 30/4 = 7
numbers but
doesn’t give
decimal
points
MOD(modulus/modulo) Return the 30/4 = 7
remainder of remainder 2
a division
It is written as
30MOD4=2
^ (exponentiation) To the power 3^3 is 27
of
Retentional operators
== means equal to
< means less than
> means more than
Rashmitha Jayawardhana Computer science 9/10 LESSON NOTES
>= means more than or equal to
<= means less than or equal to
!= means not equal
[Link] Flowcharts
- it is a visual representation of an algorithm
- It can be written up to down or left to right as well
- There are specific symbols to use when writing
Begin / end
To start or end flowcharts
Input/ output Get values or display values
Process
Does any process
Decision
Does any decision
Rashmitha Jayawardhana Computer science 9/10 LESSON NOTES
Shows the flow of an algorithm
And When 2 values are true
Or When 1 value is true out of 2
No Neither value is true
[Link] Pseudocode
A pseudocode is a simple way of writing an algorithm
- There are many regulation and keywords
Function HOW? EXAMPLE
To input RECEIVE variable RECEIVE NUM
FROM KEYBOARD FROM
KEYBOARD
To output SEND variable TO SEND date TO
DISPLAY DISPLAY
Assign values SET variable to value Set Num TO 0
IF date== 0 THEN
Selection IF condition THEN SEND “hi” TO DISPLAY
[space] statement ELSE
SEND “bye” TO DISPLAY
ELSE END IF
[space] statement
END IF
Rashmitha Jayawardhana Computer science 9/10 LESSON NOTES
IF condition THEN IF date== 0 THEN
Nested if SEND “hi” TO DISPLAY
[space] statement
ELSE IF date==1 THEN
ELSE IF condition THEN SEND “bye” TO DISPLAY
[space] statement END IF
END IF
END IF
Iteration FOR value FROM start FOR day FROM 1 TO 5
SEND “DAMN” TO
value TO end value DISPLAY
END FOR
Statement
END FOR
Constant variable CONST(variable) -none-
NOTE IF AN SELECTION OR NESTED
SELECTION IS USED YOU MUST WRITE ”END
IF” TO THE NUMBER OF TIMES YOU
OPPENED A CONDITION
Rashmitha Jayawardhana Computer science 9/10 LESSON NOTES
Rashmitha Jayawardhana Computer science 9/10 LESSON NOTES