Problem Solving & Algorithms Lesson
Problem Solving & Algorithms Lesson
Problem Solving
❖ Mathematical problems such as calculating a sale
invoice
❖ Search for patient information at a hospital
3. Print total
Step 2-Propose and evaluate
solutions
Once a solution is found, we can then review it to
see how it can be optimized or made more
efficient.
❖ Introduce possible solutions
❖ Compare solutions using advantages and
disadvantages
Step 3- Determination of the most
efficient solution
❖ We may have to go through a series of refinements
in order to get the most efficient solution.
Step 4- Develop and represent
algorithm
❖ The solution outlined in step 3 is then expanded
into an algorithm. The use of a pseudocode or
flowchart maybe used to implement the algorithm.
Step 5 – Test and Validate the
solution
❖ This is one of the most important steps. The main
purpose of desk checking the algorithm is to
identify major logic errors early, so that they may
be easily corrected.
What is an algorithm
❖ It is a set of detailed, unambiguous and ordered
instructions developed to describe the processes
necessary to produce the desired output from a
given input.
❖ Characteristics:
✔ Finite steps
✔ Precise instructions
✔ Unambiguous
✔ Flow of control from one process to another
Algorithm
❖ In simple terms, an algorithm is a series of
instructions to solve a problem (complete a task)
❖ Name as string
❖ Avg as real
Input Statement
❖ Allows the user/programmer to input data into the
algorithm
❖ provide the means of transferring data from
external media to internal process
❖ Input statement is written with Keyword and
variable.
❖ E.g. Read Num
Input Name
Practice Questions
❖ Write an instruction to accept the price and colour
of a car.
Ans: Read Price, colour
❖ Write an instruction to input three numbers.
❖ Write an instruction to read the names and ages of
two students.
❖ Write an instruction to read the radius of circle.
Parts of an algorithm
❖ Input statement
❖ Output statement
❖ Process (assignment & Arithmetic)
❖ Control process (If structure & Loop Structure)
Assignment & Calculation
❖ This statement is used to store a value into a
variable.
❖ The variable on the left (Lvalue) take the value of
the variable on the right (Rvalue)
Eg.
Sum = 0
❖ 0 Memory location
Assignment and Arithmetic
operators
❖ The arithmetic operators are:
+ Addition
- Subtraction
* Multiplication
/ Division
Mod or % Modulus (remainder)
❖ These operators can be used with simple
expressions (e.g. variables, literal values) to form
compound expressions
Assignment statement
❖ On the previous slide, we saw a couple of
examples of perfectly legal statements that don’t
make sense algebraically:
num = num + 1
num = 15 / num
❖ Remember, the operator “=” is pronounced “gets,”
not “equals,” in algorithm
– The expression to the right of the operator is evaluated
first
– The variable to the left of the operator gets, or is
assigned to store, the value of the expression
Assignment statement
❖ Once a variable is declared, it can be assigned
values multiple times in a program; for example:
B = 15
B = A
Sum = B+A
Example
❖ x = 4, y = 9, z
❖z=x+y*2 // result is 22
❖ z = (x + y) * 2 // result is 26
❖y=y–1 // result is 8
❖ Z = y mod x // result is 0
Practice questions - Teacher
❖ Practice writing input, assignment and calculation
instructions
Questions
1. Write an algorithm to accept two number and
find their total.
2. Write an algorithm to accept the length of a
square and calculate the area.
Practice questions - Students
❖ Write an algorithm to accept three numbers and
find their average.
❖ Write an algorithm to accept the diameter of a
circle and calculate the circumference
Practice questions - Students
1. Write an algorithm to accept the days in a month
and calculate the number of hours in that month
Practice questions - Students
❖ Write an algorithm to accept the diameter of a
circle and calculate the area of the circle.
Practice questions - Students