COS 102 Lecture Note 3
COS 102 Lecture Note 3
ng
COS 102
Steps involved in Algorithm
Development
Lecture Note 3
Lecturer:
ASS. PROF O. N. AKANDE
Akande Noah O. (Ph. D.)
Step 1: Identify the Input
▪ Input refers to the data or information that an algorithm
needs to operate on to produce the desired output.
▪ Inputs can come from various sources like users, sensors,
files, or other programs.
▪ Inputs determine the scope and limitations of the algorithm.
▪ Accurate identification of inputs helps prevent errors and
improves the efficiency of the algorithm.
▪ Inputs help in defining the boundaries of the problem.
2
How to Identify Inputs
▪ Analyze the Problem Statement: Carefully read the
problem to understand what information is required to
achieve the objective.
▪ Ask Key Questions:
– What information is necessary to solve the problem?
– What data will the user provide?
– Are there any constraints or conditions to consider?
▪ Determine Data Types: Consider whether the inputs
are integers, strings, floating-point numbers, etc
3
Step 2: Identify the Output
▪ The output is the result or solution the algorithm
aims to achieve after processing the given inputs.
▪ It is the goal of the problem: the reason why the
algorithm is being developed in the first place.
4
Why Identify the Output?
▪ Identifying the output helps define the purpose of the
algorithm clearly.
▪ It guides the logic of the algorithm to ensure it meets
the requirements of the problem.
▪ It helps determine the format and presentation of the
solution
▪ Format: Integer, floating point
▪ Presentation: Output on screen, store in a file, etc.
5
How to Identify the Output
▪ Identifying the output helps define the purpose of the
algorithm clearly.
▪ It guides the logic of the algorithm to ensure it meets
the requirements of the problem.
▪ It helps determine the format and presentation of the
solution
▪ Format: Integer, floating point, text or Image
▪ Presentation: Output on screen, store in a file, etc.
6
Step 3: Identify the Processing Operations
▪ Processing operations are the calculations, logical
decisions, and steps needed to convert inputs into
the desired output.
▪ They are the core actions performed on the input
data to achieve the output.
7
Why Identify Processing Operations?
▪ Processing operations bridge the gap between
the input and the output.
▪ They ensure that all necessary steps are taken to
solve the problem accurately.
▪ Identifying these operations helps avoid errors and
ensures a logical flow in the algorithm.
8
Types of Processing Operations:
▪ Arithmetic Operations: Addition (+), subtraction (-),
multiplication (*), division (/), modulus (%).
▪ Logical Operations: AND (&&), OR (||), NOT (!).
▪ Comparison Operations: Greater than (>), less than
(<), equal to (==), not equal to (!=).
▪ Conditional Operations: If-else statements, switch-
case.
▪ Loops: For, while, and do-while loops for repeated
tasks.
9
How to Identify Processing Operations:
▪ To effectively identify the required operations, you
should yourself the following questions:
▪ What calculations are necessary?
▪ Are there any decisions to make? (e.g., if-else
conditions)
▪ Are there repetitive tasks? (e.g., summing multiple
values)
▪ Are there any comparisons or evaluations needed?
10
Steps involved in Algorithm Development
▪ Step 4: Processing Definiteness: this involves
ensuring the instructions composing the algorithm
are clear and not ambiguous.
▪ Step 5: Processing Finiteness: this involves
ensuring the algorithm has finite number of steps.
▪ Step 6: Possessing Effectiveness: this involves
ensuring the algorithm cold be used to eventually
achieve the aim (result) of the problem
11
Problem Examples
▪ "A carpenter needs to determine the amount of
wood required to make a rectangular tabletop. To
do this, the carpenter must calculate the area of the
rectangle. Write an algorithm that can help the
carpenter find the area by considering the
necessary dimensions of the rectangle."
12
Examples on Input Identification
▪ "A carpenter needs to determine the amount of wood
required to make a rectangular tabletop. To do this, the
carpenter must calculate the area of the rectangle.
Write an algorithm that can help the carpenter find the
area by considering the necessary dimensions of the
rectangle.“
▪ Input: length and width of the rectangle
▪ Process: Multiplication (length x width)
▪ Output: Area (float)
13
Example 2
▪ "A teacher wants to calculate the average score of a student
in five subjects to determine their overall performance.
Write an algorithm that accepts the scores for each subject
and computes the average score.“
▪ Input: Scores for each of the five subjects
▪ Process: Addition and Division
S1+S2+ S3+ S4+S5
▪
5
▪ Output: Average Scores
14
Example 3
▪ A game developer needs a quick way to determine
whether a player's score is an even or an odd
number. Write an algorithm that takes a score as
input and checks if it is even or odd."
▪ Input: Individual player Score
▪ Process: Modulus (%)
▪ Output: Even/Odd (Text)
15
Example 4
▪ "A bank wants to develop a system to calculate the simple
interest for its customers. Write an algorithm that takes the
principal amount, rate of interest, and time (in years) as
inputs and computes the simple interest.“
▪ Input: Principal Amount, Rate of Interest, and Time (in
years)
▪ Process: Multiplication and Division
Principal×Rate×Time
▪
100
▪ Output: Floating Point Number
16
Example 5
▪ "A teacher needs to assign a grade to a student based
on their total marks out of 100. Write an algorithm that
takes the student's score as input and determines the
grade according to the following criteria:
▪ A: 90-100
▪ B: 80-89
▪ C: 70-79
▪ D: 60-69
▪ F: Below 60"
17
Example 6
▪ "A weather app needs to convert the temperature
from Celsius to Fahrenheit to Output it for users in
different regions. Write an algorithm that accepts
the temperature in Celsius and converts it to
Fahrenheit."
18
Example 7
▪ "A construction company wants to calculate the
perimeter of a triangular plot. Write an algorithm
that takes the lengths of the three sides of the
triangle as inputs and computes the perimeter."
19
Example 8
▪ "Write an algorithm that accepts a person's weight
(in kilograms) and height (in meters) as inputs and
calculates their Body Mass Index (BMI) using the
formula:
▪ BMI = weight / (height * height)
20
Example 9
▪ "Write an algorithm that accepts a year as input
and determines whether it is a leap year."
21
Actual Algorithm Development
▪ Problem Statement: Write an algorithm that takes the
length and width of a rectangle as inputs and calculates its
area.
▪ Start
▪ Input: Length and Width
▪ Compute Area using the formula:
▪ Area= Length × Width
▪ Output: the Area
▪ Stop
22
Actual Algorithm Development
▪ Write an algorithm that accepts the scores of a student in five subjects
and calculates the average score.
▪ Begin
▪ Input: Score1, Score2, Score3, Score4, Score5
▪ Compute the Sum of the scores:
▪ Sum = Score1 + Score2 + Score3 + Score4 + Score5
▪ Compute the Average using the formula:
▪ Average = 𝑺𝒖𝒎Τ𝟓
▪ Output: the Average
▪ End
23
Actual Algorithm Development
▪ Write an algorithm that takes a number as input and checks
if it is even or odd.
▪ Start
▪ Input: Number
▪ If Number % 2 == 0, then
– Output "The number is Even"
▪ Else
– Output "The number is Odd"
▪ Stop
24
Actual Algorithm Development
▪ Write an algorithm that takes a number as input and checks
if it is even or odd.
▪ Start
▪ Input: Number
▪ If Number % 2 == 0, then
– Output "The number is Even"
▪ Else
– Output "The number is Odd"
▪ Stop
25
Actual Algorithm Development
▪ A bank wants to develop a system to calculate the simple interest for its
customers. Write an algorithm that takes the principal amount, rate of
interest, and time (in years) as inputs and computes the simple interest.
▪ Start
▪ Input: Principal, Rate, Time
▪ Compute Simple Interest using the formula:
Simple Interest = Principal×Rate×Timeൗ
▪ 𝟏𝟎𝟎
▪ Output the Simple Interest
▪ Stop
26
Actual Algorithm Development
▪ A bank wants to develop a system to calculate the simple interest for its
customers. Write an algorithm that takes the principal amount, rate of
interest, and time (in years) as inputs and computes the simple interest.
▪ Start
▪ Input: Principal, Rate, Time
▪ Compute Simple Interest using the formula:
Simple Interest = Principal×Rate×Timeൗ
▪ 𝟏𝟎𝟎
▪ Output the Simple Interest
▪ Stop
27
Actual Algorithm Development
▪ A teacher needs to assign a grade to a student based on their total marks out of 100.
Write an algorithm that takes the student's score as input and determines the grade.
▪ Start
▪ Input: Score
▪ If Score >= 90, then Grade = 'A'
▪ Else if Score >= 80, then Grade = 'B'
▪ Else if Score >= 70, then Grade = 'C'
▪ Else if Score >= 60, then Grade = 'D'
▪ Else Grade = 'F'
▪ Output: Grade
▪ Stop
28
Actual Algorithm Development
▪ A weather app needs to convert the temperature from
Celsius to Fahrenheit. Write an algorithm that accepts the
temperature in Celsius and converts it to Fahrenheit.
▪ Start
▪ Input: Celsius
▪ Compute Fahrenheit using the formula:
▪ Fahrenheit = Celsius × ( 9Τ𝟓 ) + 32
▪ Output: Fahrenheit
▪ Stop
29
Actual Algorithm Development
▪ A construction company wants to calculate the perimeter of
a triangular plot. Write an algorithm that takes the lengths of
the three sides of the triangle as inputs.
▪ Start
▪ Input: Side1, Side2, Side3
▪ Compute Perimeter = Side1 + Side2 + Side3
▪ Output: Perimeter
▪ Stop
30
Actual Algorithm Development
▪ Write an algorithm that accepts three different numbers as inputs
and determines the largest among them.
▪ Start
▪ Input: Num1, Num2, Num3
▪ If Num1 > Num2 and Num1 > Num3, then Largest = Num1
▪ Else if Num2 > Num1 and Num2 > Num3, then Largest = Num2
▪ Else Largest = Num3
▪ Output: Largest
▪ Stop
31
Actual Algorithm Development
▪ Write an algorithm that takes a person's age as
input and checks if they are eligible to vote. The
minimum age required to vote is 18 years.
▪ Start
▪ Input: Age
▪ If Age >= 18, Output "Eligible to vote"
▪ Else, Output "Not eligible to vote"
▪ Stop
32
Actual Algorithm Development
▪ Write an algorithm that accepts a person's weight (in
kilograms) and height (in meters) as inputs and calculates
their BMI.
▪ Start
▪ Input: Weight and Height
▪ Compute BMI using the formula:
𝑾𝒆𝒊𝒈𝒉𝒕
▪ BMI = ൗ(𝑯𝒆𝒊𝒈𝒉𝒕∗𝑾𝒆𝒊𝒈𝒉𝒕 )
▪ Output: BMI
▪ Stop
33
Actual Algorithm Development
▪ Write an algorithm that accepts a year as input and determines
whether it is a leap year.
▪ Start
▪ Input: Year
▪ If Year is divisible by 4 and not divisible by 100, or divisible by
400, then it is a Leap Year
▪ Else, it is not a Leap Year
▪ Output: the result
▪ Stop
34