Algorithms
Algorithms
2 Algorithms
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.
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)
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)
For example, a program might be written to sort an array using the divide and conquer approach.
Þ Given array
Þ 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.
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
? 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.
Decision Symbol (diamond) – denotes a point in the program where more than one
path can be taken.
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 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
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)
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
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
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.
START
PASSED or FAILED
Grade = 87
Is No
87 < 75
NO Grade < 75 YES
PRINT “PASSED” PRINT “FAILED”
?
PASSED
END END
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.
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.
______ 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