Algorithm
32
• A formula or set of steps for solving a particular problem.
• To be an algorithm, a set of rules must be unambiguous and
have a clear stopping point.
• Algorithms can be expressed in any language, from natural
languages like English
• We use algorithms every day. For example, a recipe for baking
a cake is an algorithm. Most programs, with the exception of
some artificial intelligence applications, consist of algorithms.
• Inventing elegant algorithms -- algorithms that are simple
and require the fewest steps possible -- is one of the principal
challenges in programming.
Examples of algorithm
33
• Cookbook
• Recipe Booklet
• Choreography
• In the same way, algorithms executed by computer can
combine millions of elementary steps, such as
additions, subtractions, multiplications and divisions
into complicated mathematical calculations.
• In the same way, algorithms executed by computer can
combine millions of elementary steps, such as
additions, subtractions, multiplications and divisions
into complicated mathematical calculations.
Examples of algorithm
34
Here is one algorithm for the usual process of saving a document
in a disk.
1. Find a new or unused disk.
2. Insert the disk in drive G.
3. From the word environment, select file menus then select
save as.
4. From the save as dialog box, select the directory where you
save the document.
5. Position the pointer inside the filename textbox then type the
filename of the document.
6. Then click the save button to formally save the document.
Problems (1,2,3,4,5)
35
1. Create an algorithm on how to kill a chicken.
2. Create an algorithm on how to prepare a cup of coffee.
3. Create an algorithm on how to cook pinakbet.
4. Create an algorithm on how to get the sum of two numbers.
5. Create an algorithm on how to get the average of the three
(3) numbers.
Flowchart
36
A flowchart is a graphical or symbolic representation of
a process. Each step in the process is represented by a
different symbol and contains a short description of the
process step.
The flow chart symbols are linked together with arrows
showing the process flow direction.
Basic Symbols
37
• A flowchart is a graphical or symbolic representation
of a process.
• Each step in the process is represented by a
different symbol and contains a short description of
the process step.
• The flow chart symbols are linked together with
arrows showing the process flow direction.
Basic Symbols
38
Basic Symbols
39
Basic Symbols
40
Basic Symbols
41
Basic Symbols
42
Basic Symbols
43
Basic Symbols
44
45
Example
of
Flowchart
Notations Used in Flowchart
46
Notation Meaning
: comparison
& logical and
Y yes
N no
EOF End of File
Relational Test Operators
47
Notation Meaning
> is greater than
< is less than
<= or =< is less than or equal to
>= or => is greater than or equal to
<> or >< is not equal to
= is equal to
Arithmetic Operators
48
Notations Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
() Grouping
** or ^ Exponentiation
Precedence of the Operators
49
The computer follows this order of
priority
1. Grouping ()
2. Exponentiation
3. Multiplication or division
4. Addition or subtraction
When operators are all of equal priority, the
computer evaluate the expression from left
to right.
50
When operators are of different priorities, the
computer performs those operations with higher
priorities first
51
Operations enclosed in parentheses will take place
before other operations. Note (if there are multiple
parentheses, the innermost parenthesis will be
52
evaluated first).
Write clearly the computation desired
53
Example #2.3
2L / 3G
Answer?
( 2 * L ) / (3 * G )
Problem 6 and 7
54
6. Create a flowchart that would ask for a legal age to
access a particular porn site. If age is greater than or
equal to 18 then print “Yehey, you are allowed, happy
Viewing”, otherwise, print “Sorry, you can’t”.
7. Create a flowchart that would determine if
the grade of student is Passed or Failed. If grade
is greater than or equal to 75, print “Passed”,
else “Failed”.
Problem 8
55
Create a flowchart that would get the
average of student’s grade from 5
subjects (Math, Science, Social Sciences,
Biology, P.E) and determine whether the
average is passed or failed. If the average
is greater than 75, display “Passed”,
otherwise display “Failed”.
Loops and Counters
56
• Looping is used when it is desired to
make the same calculation on more than
one set of data. It consists of repeating a
program, or a section of a program, and
substituting new data for each repetition.
• A counter is set up in a program loop to
keep track of the number of times the
program segment is repeated.
Steps in Loop Control
57
1. Initialization – the value of a counter used is initially
set equal to zero (or one). This process is
always done outside the loop.
2. Test for Limit Conditions – before logic flow gets out
of the loop, a loop – terminating condition
must first be satisfied. The process of
testing is usually found either at the
beginning or at the end of a loop.
3. Incrementation – after each loop is executed, 1 is
added to the counter. Thus the counter
reflects the number of times the operation
has been performed. This process is always
done within the loop.
The algorithm for this particular requirement
could be listed as follows:
58
1. Initialize the counter (ctr) and accumulator (sum) to
zero.
2. Read in student’s name and score.
3. Print out the student’s name and score.
4. Accumulate the data value (score) into sum.
5. Test if the desired amount of data (40) has been
reached.
6. If the value of counter (ctr) is less than 40, repeat
steps 2 through 5. However, if the desired amount
of data has been reached (40), compute the average
score by dividing the sum by the value of counter.
7. Print out the average score.
Equivalent flowchart of the problem
59
Problem 9 and 10
60
9. Create a flowchart that would change the
initial value of the variable X which is 10 to
100 when the value of variable Y is 50.
10. Draw a flowchart that would convert the a
particular number to words. Numbers may be from
1 to 5. Example, if the value is 1, then it will display
the word “One”, “Two” for 2 and so on…
Problem 11
61
Find the amount to charge people of varying ages for a food
ticket. When the person is under 16, the charge is Php 7.00,
when the person is 65 or over, the charge is Php 5.00, all others
are charged Php 10.00. The conditions are the following:
Age Charge
<16 7
>=16 and <65 10
>=65 5