CHAPTER
2 Algorithms
In mathematics and computer science, an algorithm is a step-by-step procedure to solve a problem or to
perform a task. One of the most obvious examples of an algorithm is a recipe.
Learning Outcomes
At the end of this Chapter, the students must have:
ü analyzed problems and defined the computing requirements appropriate to its solution;
ü analyzed user needs and take them into account in the selection, creation, evaluation and administration of
computer-based systems; and
ü created a portfolio covering the types of algorithm.
I Pretest
MUTIPLE CHOICE: Write only the letter (UPPERCASE LETTER) that corresponds to your answer. Write your
answer to the space provided before each number.
______ 1. What is an algorithm?
a) A chart showing the flow of a series of events
b) A decision arrived at by following instructions
c) Step-by-step instructions used to solve a problem
d) A computer program that follows a chart
______ 2. If something needs to be processed what flowchart symbol do we use?
a) oval
b) rectangle
c) parallelogram
d) diamond
______ 3. What shape in the flowchart represents a decision?
a) rectangle
b) diamond
c) oval
d) parallelogram
______ 4. What does an oval represent in a flowchart?
a) decision making
b) start
c) stop
d) process
______ 5. In a flowchart how are symbols connected?
a) Symbols do not get connected together in a flowchart
b) With lines and an arrow to show the direction of flow
c) With dashed lines and numbers
d) With solid lines to link events
Lesson 1 Input-Process-Output (IPO)
Input-Process-Output (IPO) model, is a widely used approach in systems analysis and software engineering
for describing the structure of an information processing program or other process.
Figure 2.1 IPO model
A computer program or any other sort of process using the input-process-output model receives inputs from
a user or other source, does some computations on the inputs, and returns the results of the computations. The
system divides the work into three categories:
1. input - data that is needed to solve the problem
2. process - the task that will be carried out to solve the problem
3. output - the end result of the problem
For example, a program might be written to compute the price of apples if the quantity in kg and price per kg
are given. Following the IPO model, the program must:
Þ Ask the user for the weight (quantity in kg) of apple and price per kilogram (input)
Þ Perform a calculation to compute the total price of apple (process)
Þ Display the total price (output)
Quantity Price = Quantity * PricePerKilo Price
PricePerKilo
Second example, compute the sum of three numbers, the program must:
Þ Ask the user for the three number (input)
Þ Perform a calculation to compute the sum (process)
Þ Display the sum (output)
Num1 Sum = Num1 + Num2 + Num3 Sum
Num2
Num3
Third example, compute the area of a rectangle, the program must:
Þ Ask the user for the length and width (input)
Þ Perform a calculation to compute the area (process)
Þ Display the area (output)
Length Area = Length * Width Area
Width
? Exercises
Write the IPO algorithm of the following problems:
Þ Compute and print the area and perimeter of the square using the given side.
Þ Write a program to calculate the average grade of three students.
Þ Convert Fahrenheit temperatures into Celsius temperatures.
Þ Convert kilogram into pounds.
Þ Convert meter into inch.
Þ Convert kilometer into mile.
Þ Calculate the car park charges: The 1st three hours costs ₱20.00. The subsequent hours cost ₱5.00
per hour. Write an algorithm based on a vehicle’s entry and exit time.
Lesson 2 Divide and Conquer
A divide-and-conquer algorithm works by recursively breaking down a problem into two or more sub-
problems of the same or related type, until these become simple enough to be solved directly. The solutions to the
sub-problems are then combined to give a solution to the original problem (shown in Figure 2.2).
Figure 2.2 Divide and Conquer Diagram
Þ Divide - break the given problem into smaller sub-problems.
Þ Conquer - Solve the smaller sub-problems. If the sub-problem is small enough, then solve it directly.
Þ Combine - Merge the solutions of the sub-problems to get the solution to the actual or original problem.
Sample Task: Prepare a Breakfast
Step 1: Step 2: Step 3:
1. Start 1. Start 1. Start
2. Prepare a Breakfast 2. Prepare a Breakfast 2. Prepare a Breakfast
3. End 2.1 Prepare a tuna sandwich 2.1 Prepare a tuna sandwich
2.2 Prepare some chips 2.1.1 Take 2 slices of bread
2.3 Make a cup of coffee 2.1.2 Prepare tuna paste
3. End 2.2 Prepare some chips
2.3 Make a cup of coffee
3. End
Step 4: Step 5:
1. Start 1. Start
2. Prepare a Breakfast 2. Prepare a Breakfast
2.1 Prepare a tuna sandwich 2.1. Prepare a tuna sandwich
2.1.1 Take 2 slices of bread 2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste 2.1.2 Prepare tuna paste
2.2 Prepare some chips 2.2. Prepare some chips
2.2.1 Cut potatoes into slices 2.2.1 Cut potatoes into slices
2.2.2 Fry the potatoes 2.2.2 Fry the potatoes
2.3 Make a cup of coffee 2.3. Make a cup of coffee
3. End 2.3.1 Boil water
2.3.2 Add water, sugar and
coffee
3. End
For example, a program might be written to sort an array using the divide and conquer approach.
Þ Given array
Þ Divide the array into two halves
Þ Again, divide each subpart recursively into two halves until you get individual elements.
Þ Then, merge the individual elements in a sorted manner. Here, conquer and combine steps go side by
side.
Lesson 3 Pseudocode
Pseudocode is an outline of a program, written in a form that can easily be converted into real programming
statements. It resembles the actual program that will be implemented later. However, it cannot be compiled nor
executed.
Pseudocode normally codes the following actions:
Þ Initialization of variables
Þ Assignment of values to the variables
Þ Arithmetic operations
Þ Relational operations
For example, a program might be written to compute the price of apples if the quantity in kg and price per kg
are given. The program must:
Þ Ask the user for the weight (quantity in kg) of apple and price per kilogram (input)
Þ Perform a calculation to compute the total price of apple (process)
Þ Display the total price (output)
a. Start
b. Read Quantity
c. Read PricePerKg
d. Price = Quantity * PricePerKg
e. Print Price
f. End
Second example, compute the sum of three numbers, the program must:
Þ Ask the user for the three number (input)
Þ Perform a calculation to compute the sum (process)
Þ Display the sum (output)
a. Start
b. Read N1
c. Read N2
d. Read N3
e. Sum = N1 + N2 + N3
f. Print Sum
g. End
Third example, compute the area of a rectangle, the program must:
Þ Ask the user for the length and width (input)
Þ Perform a calculation to compute the area (process)
Þ Display the area (output)
a. Start
b. Read Length
c. Read Width
d. Area = Length * Width
e. Print Area
f. End
? Exercises
Write the Pseudocode of the following problems:
Þ Compute and print the area and perimeter of the square using the given side.
Þ Write a program to calculate the average grade of three students.
Þ Convert Fahrenheit temperatures into Celsius temperatures.
Þ Convert kilogram into pounds.
Þ Convert meter into inch.
Þ Convert kilometer into mile.
Þ Calculate the car park charges: The 1st three hours costs ₱20.00. The subsequent hours cost ₱5.00
per hour. Write an algorithm based on a vehicle’s entry and exit time.
Lesson 4 Flowchart
A flowchart is a diagram representing the logical sequence in which a combination of steps or operations is
to be performed. It consists of labeled geometrical symbols that are interconnected. It is also a visual
representation of an algorithm. It is intended for communication and documentation.
Flowchart Symbols:
Terminal Symbol (oval) – use to designate the beginning and the end of the program.
Input/output Symbol (parallelogram) – represents an instruction to an input or an
output device.
Processing Symbol (rectangle) – represent a group of program instructions that
perform a processing function of the program such as to perform arithmetic
operations.
Decision Symbol (diamond) – denotes a point in the program where more than one
path can be taken.
Preparation Symbol (hexagon) – represent an instruction or group of instructions that
will alter, or modify a program’s course of execution. It is commonly used to
specify operations such as control, index register, initialization, switch setting,
and in indicating loops.
On-page Connector (small circle) – a none processing symbol use to connect one part
of a flowchart to another without drawing flow lines.
Off-page Connector (small pentagon) – to designate entry to or exit from a page when
a flowchart requires more than one page.
Flow Direction Indicators (arrowheads) – use to show the direction of processing or
data flow. These are added to flow lines if a flowchart appears confusing is its
layout. Arrowheads are not required when the logic flow is from top to bottom or
from left to right.
Flow lines (horizontal/vertical lines) – use to show reading order or sequence in which
flowchart symbols are to be read. Flow lines are sometimes drawn with
arrowheads. The commonly accepted practice is to indicate an arrowhead if
the logic flow is from right to left or from bottom to top.
Table 2.1 Arithmetic and Relational Operators
Arithmetic Operators Relational Operators
OPERATORS MEANING OPERATORS MEANING
+ Addition < Less than
- Subtraction > Greater than
* Multiplication = Equal
/ Division ≤ Less than or equal
Div Division integer ≥ Greater than or equal
Mod Modulus (remainder) ≠ Not equal
For example, a program might be written to compute the price of apples if the quantity in kg and price per kg
are given. The program must:
Þ Ask the user for the weight (quantity in kg) of apple and price per kilogram (input)
Þ Perform a calculation to compute the total price of apple (process)
Þ Display the total price (output)
Figure 2.3 Simple Flowchart
START START
Note: You can combine
inputs into one symbol.
Use comma to separate
READ Quantity
two or more variables READ Quantity,
PricePerKg
READ PricePerKg
Price = Quantity * PricePerKg
Price = Quantity * PricePerKg
PRINT Price
PRINT Price
END
END
Second example, compute the sum of three numbers, the program must:
Þ Ask the user for the three number (input)
Þ Perform a calculation to compute the sum (process)
Þ Display the sum (output)
START Compute the Area of a rectangle
START
READ N1, N2, N3 N1 = 11
N2 = 4
N3 = 7
Sum = N1 + N2 + N3
Area = 11 + 4 + 7
= 22
PRINT Sum
22
END END
Third example, compute the area of a rectangle, the program must:
Þ Ask the user for the length and width (input)
Þ Perform a calculation to compute the area (process)
Þ Display the area (output)
Compute the Area of a rectangle
START
START
Length = 10
Width = 5
READ Length, Width
Area = 10 * 5
= 50
Area = Length * Width
50
PRINT Area
END
END
? Exercises
Draw the flowchart of the following problems:
Þ Compute and print the area and perimeter of the square using the given side.
Þ Write a program to calculate the average grade of three students.
Þ Convert Fahrenheit temperatures into Celsius temperatures.
Þ Convert kilogram into pounds.
Þ Convert meter into inch.
Þ Convert kilometer into mile.
Þ Calculate the car park charges: The 1st three hours costs ₱20.00. The subsequent hours cost ₱5.00
per hour. Write an algorithm based on a vehicle’s entry and exit time.
Flowchart with Decision
For example, a program might be written to accept grade and print “FAILED” if grade is less than 75,
otherwise print “PASSED”. The program must:
Þ Ask the user for the grade (input)
Þ Display “FAILED” if grade is less than 75, otherwise display “PASSED” (process and output)
START
PASSED or FAILED
READ Grade START
Grade = 87
Is No
87 < 75
NO Grade < 75 YES
PRINT “PASSED” PRINT “FAILED”
?
PASSED
END END
If the inputted grade is 87, then the remarks will be PASSED.
You can also use the on-page connector (small circle) to connect one part of a flowchart to another instead
of a long flow line.
.
START
READ Grade
Is
NO YES
PRINT “PASSED” Grade < 75 PRINT “FAILED”
?
A
A END
Second example, draw a flowchart to get a number from the user and print whether it is positive or negative,
the program must:
Þ Ask the user for a number
Þ If a number is less than zero then print “NEGATIVE”, otherwise print “POSITIVE”.
START
READ Num
Is
NO YES
PRINT “POSITIVE” Num < 0 PRINT “NEGATIVE”
?
A
A END
Third example, draw a flowchart to get a number from the user and print whether it is even or odd, the
program must:
Þ Ask the user for a number
Þ If a number divided by 2 is zero remainder then print “EVEN”, otherwise print “ODD”.
START
READ Num
Is
NO YES
PRINT “ODD” Num mod PRINT “EVEN”
2=0?
A
A END
Forth example, draw a flowchart to get two numbers from the user and print the smaller number, the
program must:
Þ Ask the user for the two numbers
Þ If first number is less than the second number, print the value of first number, otherwise print the value of
the second number.
START
READ N1, N2
NO Is YES
PRINT N2 N1 < N2 PRINT N1
?
A
A END
? Exercises
Draw the flowchart of the following problems:
Þ Take three numbers from the user and print the biggest number.
Þ Takes a year from user and print whether that year is a leap year or not.
Þ Takes an age from the user and print whether the age is minor or adult. Minor age is below 18.
Þ Calculate the car park charges: The 1st three hours costs ₱20.00. The subsequent hours cost ₱10.00
per hour. Write an algorithm based on a vehicle’s entry and exit time.
Flowchart with Loop
Þ Loop – a sequence of steps to be executed a specified number of times.
Þ Counter – is set up in a program loop to keep track of the number of times the program segment is
repeated.
For example, make a flowchart the will print numbers 1 to 20
Þ Step 1: Start
Þ Step 2: C=1, initialization of counter
Þ Step 3: Yes, C is less than or equal to 20
START
Þ Step 4: Print the value of C
Þ Step 5: C = C + 1, value of counter will increase by 1
C=1
Þ Step 6: Go to Step 3
A
Þ Step 7: No, C is not less than or equal to 20
Þ Step 8: End
IS
NO C<=20 YES PRINT C
END
?
C=C+1
A
Second example, compute and print the sum of numbers 1 to 20
Þ Step 1: Start
Þ Step 2: C=1, initialization of counter
Þ Step 3: Sum = 0, initialization of sum
Þ Step 4: Yes, C is less than or equal to 20
Þ Step 5: Sum = Sum + C, add the value of the counter to the sum
Þ Step 6: C = C + 1, value of counter will increase by 1
Þ Step 7: Go to Step 4
Þ Step 8: No, C is not less than or equal to 20
Þ Step 9: Print the value of Sum
Þ Step 10: End
START
C=1
Sum = 0
A
IS
PRINT Sum NO C<=20 YES Sum = Sum + C
? C=C+1
END
A
? Exercises
Draw the flowchart of the following problems:
Þ Take ten numbers from the user and print the biggest number.
Þ Compute and print the sum of all even and all odd numbers of numbers 1 to 10.
Þ Print the smallest and the largest number in ten (10) inputted integers.
Þ Determine if the inputted integer is a prime number or not.
Þ Compute and print the average grade of fifty (50) students.
7 Learning Activities
Activity 1: Algorithms Portfolio
Directions: Create a portfolio covering all lesson activities in the algorithm.
Amazing! You may now take the assessment. If you have
not completed the task, or you have difficulty in accomplishing
the ac vity, please send me a message to our google class or
you may ask clarifica ons through a text message or phone
calls on the contact number included in your course guide.
ü Chapter Test
GENERAL RULES: Write your answer to the space provided before each number.
I – MUTIPLE CHOICE: Write only the letter (UPPERCASE LETTER) that corresponds to your answer.
______ 1. List of instruction for carrying out some process step by step.
a) algorithms
b) Interpreter
c) Compiler
d) pseudocode
______ 2. What is a diagrammatic representation that illustrates the sequence of operations to be performed to get
the solution of a problem?
a) Algorithms
b) Flowchart
c) Programming
d) Pseudocode
______ 3. What shape do flowcharts begin with?
a) rectangle
b) oval
c) diamond
d) parallelogram
______ 4. What shape do flowcharts use to show an output?
a) rectangle
b) oval
c) diamond
d) parallelogram
______ 5. What shape do flowcharts use to show a decision?
a) rectangle
b) oval
c) diamond
d) parallelogram
______ 6. What shape do flowcharts use to show an action or process?
a) rectangle
b) oval
c) diamond
d) parallelogram
______ 7. What shape do flowcharts use to show an input?
a) rectangle
b) oval
c) diamond
d) parallelogram
______ 8. What shape do flowcharts end with?
a) rectangle
b) oval
c) diamond
d) parallelogram
______ 9. It is a visual representation of an algorithm.
a) interpreter
b) flowchart
c) pseudocode
d) compiler
______ 10. What is the purpose of flow lines and arrowheads in flowchart?
a) Represents when something is input into the program or output from the program
b) Asks a question and then determines which route the program will take
c) An action done by the program (e.g. calculate the area of a square)
d) Shows the direction and sequence of processes
______ 11. A benefit of using flowcharts is ________.
a) They help you program faster
b) They write the program for you
c) They help you visualize the instructions
d) They are easier
______ 12. What is the purpose of terminal symbol?
a) Indicates the beginning or end of the flowchart
b) Indicates an action or step in the flowchart process.
c) A subroutine by user request, where the user is able to request info
d) Indicates that the program has a save function.
______ 13. Which of the following is incorrect? Algorithms can be represented:
a) as pseudo codes
b) as syntax
c) as programs
d) as flowcharts
______ 14. In a flowchart how are symbols connected?
a) Symbols do not get connected together in a flowchart
b) With lines and an arrow to show the direction of flow
c) With dashed lines and numbers
d) With solid lines to link events
______ 15. When can algorithms be used?
a) Only with computers
b) Only when programming
c) Only with flowcharts
d) Any time to design solutions to problems
II – TRUE or FALSE: Write T if the statement is true, otherwise write F.
______ 1. Algorithm refers to a special method usable for the solution to a problem.
______ 2. We can show the sequence of steps in an algorithm in a structural diagram called a flow chart.
______ 3. Only top software programmers can write an algorithm.
______ 4. When you write an algorithm the order of the instructions is very important.
______ 5. Any algorithm is a program.