CS112 - 02 - Algorithm
CS112 - 02 - Algorithm
ALGORITHM
AND PROGRAMMING CONCEPTS
1 ALGORITHMS
“
To solve any problem a plan is
needed. This plan is a procedure
to solve the problem in question.
The procedure has to be based
on definite reasoning and logic to
obtain a result.
3
WHAT IS AN ALGORITHM?
Algorithm is a step-by-step procedure in solving a problem.
4
DIFFERENT WAYS OF STATING
ALGORITHM
Algorithms may be represented in various ways.
1. Step-form
2. Pseudo-code
3. Flowchart
4. Nassi-Schneiderman
5
DIFFERENT WAYS OF STATING
ALGORITHM
Algorithms may be represented in various ways.
6
DIFFERENT WAYS OF STATING
ALGORITHM
Algorithms may be represented in various ways.
7
DIFFERENT WAYS OF STATING
ALGORITHM
Algorithms may be represented in various ways.
8
THE KEY FEATURES OF AN
ALGORITHM AND THE STEP-FORM
An algorithm for making a pot of tea:
1. If the kettle does not contain water, then fill the kettle.
2. Plug the kettle into the power point and switch it on.
3. If the teapot is not empty, then empty the teapot.
4. Place tea leaves in the teapot.
5. If the water in the kettle is not boiling, then go to step 2.
6. Switch off the kettle.
7. Pour water from the kettle into the teapot.
9
THE KEY FEATURES OF AN
ALGORITHM AND THE STEP-FORM
From the previous example, it is evident that algorithms show
these three features:
10
THE KEY FEATURES OF AN
ALGORITHM AND THE STEP-FORM
11
THE KEY FEATURES OF AN
ALGORITHM AND THE STEP-FORM
Termination – the definition of algorithm cannot be
restricted to procedures that eventually finish. Algorithms
might also include procedures that could run forever
without stopping.
Correctness – the prepared algorithm needs to be
verified for its correctness. Correctness means how easily
its logic can be argued to satisfy the algorithm’s primary
goal.
12
2 ALGORITHM v PROGRAM
ALGORTHM /VS/ PROGRAM
Domain Knowledge Programmer
Any Language Programming Language
(Hardware and/or Software)
Optional Mandatory
* Because you can use a * You can only write or
pen and paper to write an create a program with a
algorithm. computer in a specific
software.
14
3 VARIABLES
WHAT ARE VARIABLES?
Almost every algorithm contains data and usually the data is
‘contained’ in what is called a variable.
The variable is a container for a value that may vary during the
execution of the program.
For example, in the tea-making algorithm, the level of water in
the kettle is a variable, the temperature of the water is a
variable, and the quantity of tea leaves is also a variable.
16
WHAT ARE VARIABLES?
Each variable in a program is given a name, for example,
Water_Level
Water_Temperature
Tea_Leaves_Quantity
17
WHAT ARE VARIABLES?
The statement if the kettle does not contain water then fill the
kettle could also be written as
18
VARIABLES AND DATA TYPES
19
NAMING OF VARIABLES
20
NAMING OF VARIABLES
ALGORITHM IN PLAIN ENGLISH ALGORITHM USING VARIABLE NAMES
1. If the kettle does not contain water, then fill the kettle. 1. If kettle_empty then fill the kettle.
2. Plug the kettle into the power point and switch it on. 2. Plug the kettle into the power point and switch it on.
3. If the teapot is not empty, then empty the teapot. 3. If teapot_not_empty then empty the teapot.
4. Place tea leaves in the teapot. 4. Place tea leaves in the teapot.
7. Pour water from the kettle into the teapot. 7. Pour water from the kettle into the teapot.
21
4 SUBROUTINES
ACTIVITY
Write or formulate a step-form algorithm based on
daily routines or activities. E.g. Cooking a Ramen,
Watch a Netflix Show/Movie, Watch a Movie in
Theater, Buying Vegetables to the Market, etc.