www.trinityuniversity.edu.
ng
PROBLEM SOLVING
CSC 121 (3 UNITS)
Lecture 2
DR. OBABUEKI OMO-OKHIRELEN
Lecture Objectives
• Define an algorithm
• State its features, advantages and disadvantages
• Describe ways to implement algorithms
• Create flowchart to solve problems.
• Create pseudocodes solve problems
Algorithm
A step by step process for solving a problem.
An algorithm is a set of obvious, logical, and sequential
steps that solve a specific problem.
To put it simply, the algorithm is like a recipe for preparing
a specific food.
Following the steps of the algorithm will end up solving
the problem
Reason for Algorithm
A programmer writes a program to instruct the computer to do
certain tasks as desired.
The computer then follows the steps written in the program
code to solve the identified problem.
Before coding, the programmer first prepares a roadmap of the
program to be written, before actually writing the code.
Without this roadmap, the programmer may not be able to
clearly visualize the instructions to be written and may end up
developing a program which may not work as expected.
Such a roadmap is nothing but the algorithm and it is the
building block of a computer program.
Steps in Algorithm Development
Step1. Identification of input: For an algorithm, there are
quantities to be supplied called input and these are fed
externally. The input is to be identified first for any specified
problem.
Step2: Identification of output: From an algorithm, at least
one quantity is produced, called for any specified problem.
Step3: Identify the processing operations: All the
calculations to be performed in order to lead to output
from the input are to be identified in an orderly manner.
Sections of an Algorithm
Every algorithm should have the following sections, in the
stated order:
Header: Algorithm’s name or title.
Declaration: A brief description of algorithm and
variables. i.e. a statement of the purpose.
Start: A start statement
Body: Sequence of steps
Terminator: End statement
Characteristics of Algorithms
Characteristics of Algorithm An algorithm must possess the
following characteristics:
Precision — the steps are precisely stated or defined.
Uniqueness — results of each step are uniquely defined
and only depend on the input and the result of the
preceding steps.
Finiteness — the algorithm always stops after a finite
number of steps.
Input — the algorithm receives some input.
Output — the algorithm produces some output.
Advantage and Disadvantages of
Algorithms
Advantages of Algorithms
Designing an algorithm has following advantages:
1. Effective Communication: Since algorithm is written in
English like language, it is simple to understand step-by-
step solution of the problems.
2. Easy Debugging: Well-designed algorithm makes debugging
easy so that we can identify logical error in the program.
3. Easy and Efficient Coding: An algorithm acts as a blueprint
of a program and helps during program development.
4. Independent of Programming Language: An algorithm is
independent of programming languages and can be easily
coded using any high level language.
Disadvantages of Algorithms
An algorithm has following disadvantages:
1. Developing algorithm for complex problems would be time
consuming and difficult to understand.
2. Defining and understanding complex logic through
algorithms can be very difficult.
Methods for Expressing Algorithms
There are three (3) methods for expressing algorithms. They
are:
1. Natural Language Implementation
2. Pseudocode Implementation
3. Flowchart Implementation.
Natural language Representation
pf algorithms
The process of representing an algorithm's steps and
logic in a language that is readable by humans is known
as "natural language representation of an algorithm.“
This representation skips past technical programming
terminology and mathematical symbols to give a high-
level understanding of the algorithm's operation.
It makes it possible for someone with little experience
with programming to understand the general principles
of the method.
Examples Algorithms
Example 1. Write an algorithm to calculate the simple interest
using the formula:
𝑆𝑖𝑚𝑝𝑙𝑒 𝑖𝑛𝑡𝑒𝑟𝑒𝑠𝑡 = 𝑃 ∗ 𝑁 ∗ 𝑅/100, Where 𝑃 is principle
Amount, 𝑁 is the number of years and 𝑅 is the rate of interest.
Algorithm calculatesimpleinterest
Step 1:Start.
Step 2: Read the three input quantities P, N, and R.
Step 3: Calculate simple interest as
Simple interest = P* N* R/100
Step 4: Print simple interest.
Step 5: Stop.
Natural Language Algorithms Examples
Example 2: Write an algorithm to find the area of the triangle.
Let 𝑏, 𝑐 be the sides of the triangle 𝐴𝐵𝐶 and 𝐴 the included
angle between the given sides.
Algorithm findAreaOfTriangle
Step 1:Start.
Step 2: Input the given elements of the triangle namely sides 𝑏,
𝑐, and the angle between the sides 𝐴
Step 3: Area = 1/ 2 ∗ 𝑏 ∗ 𝑐 ∗ 𝑠𝑖𝑛(𝐴)
Step 4: Output the Area
Step 5: Stop.
Examples Algorithms
3. Write an algorithm to find the largest of three numbers 𝑋, 𝑌,
𝑍.
Algorithm FindLargestOfThreeNumbers
Step 1: Start
Step 2: Read 3 numbers and store in A, B, C
Step 3: Compare A and B. lf A > B then go to step 6
Step 4: Compare B and C if C > B then go to step 8
Step 5: print “B is largest” go to step 9
Step 6: Compare A and C if C > A then go to step 8
Step 7: Print ”A is largest” go to step 9
Step 8: Print “C is largest”
Step 9: Stop
Examples Algorithms
4, Write an algorithm to calculate the perimeter and area of
rectangle. Given its length and width
Algorithm calculatePerimeterAndAreaOfRectangle
Step 1:Start.
Step 2: Read length and width of the rectangle
Step 3: Calculate perimeter = 2* (length + width)
Step 4: Calculate area = length *width.
Step 5: Print perimeter
Step 6 Print area
Step 7: Stop
Examples Algorithms
5. Problem: Create an algorithm that multiplies two numbers
and displays the output.
algorithm: Multwonumbers
Step 1 Start
Step 2 Declare three integers x, y & z
Step 3 Enter values of x & y
Step 4 Multiply values of x & y
Step 5 Store result of step 4 to z
Step 6 Print z
Step 7 Stop
Flowchart
A flowchart is a pictorial or visual representation of
an algorithm.
A flowchart is a diagram made up of boxes, diamonds
and other shapes, connected by arrows.
Each shape represents a step of the solution process
and the arrow represents the order or link among the
steps.
It is a diagrammatic representation of the steps or
progression through a process.
Flowchart Symbols
Here is a list of the basic flowchart symbols you need to know.
(You will provide a list of all flowchart symbols, names and
functions as an assignment to be submitted on Friday 15th
March, 2024)
Flowchart Symbols
Example 1: Draw the flowchart to find the area of a rectangle
of sides L and W.
Flowchart Symbols
Example 1: Draw the flowchart to find the area of a rectangle
of sides L and W.