You are on page 1of 30

Developing Algorithms

(Pseudocode & Flowcharts)

27th Feb, 2024


ICT172: Principles to Programming
Lecture Notes
Objectives
1. Define algorithm
2. Mention at least five (5) properties of an algorithm
3. Differentiate between pseudocode and flowchart
4. Write pseudocode for various problems
5. Design flowchart for various problems

ICT172: Principles to Programming


Lecture Notes 2
What is an Algorithm
•An algorithm refers to logical sequence of steps used
to solve a problem.

•An algorithm therefore is made up of instructions.


•The instructions: cannot assume anything, cannot skip
steps, must be executable one step at a time, and
must be complete.

•The number of instructions in an algorithm is


determined by many factors including the way the
programmer chooses to solve the problem.
ICT172: Principles to Programming
Lecture Notes 3
Properties Algorithms
1. Input: An algorithm must have zero or more input that it takes
from an external source.
2. Output: an algorithm must have one or more output, which
represents the solution to the problem.
3. Definiteness: each step must be precisely/accurately defined.
Steps must be unambiguous
4. Correctness: An algorithm must produce the correct output for all
possible inputs and should solve the problem it was defined for.
5. Finiteness: An algorithm must terminate (produce an output)
after a finite number of steps.
6. Effectiveness: Every step in the algorithm should be executable
and should contribute to solving the problem.
ICT172: Principles to Programming
Lecture Notes 4
Ways of representing an Algorithm
•An algorithm can be represented in two common
ways:

1. Pseudocode: a description of the sequence of


steps required to solve a problem using natural
language and some programming language-like
constructs to outline the logic of a program or
algorithm.

2. Flowchart: a graphic/visual representation of the


sequence of steps required to solve a problem.

ICT172: Principles to Programming


Lecture Notes 5
Ways of Representing an Algorithm

Algorithm
Pseudocode
a description of the sequence of steps required to solve a
problem using natural language and some programming
language-like constructs.

Flowchart
a graphic/visual representation of the sequence of steps
required to solve a problem.

ICT172: Principles to Programming


Lecture Notes 6
Pseudocode
•Pseudocode is a description of the sequence of steps
involved in solving a problem. It is often written in something
similar to plain English.

• Pseudocode has no strict syntax as it is the case with


programing languages – it is not a programming language.

• Pseudocode cannot be executed on a real computer.

• It only models and resembles real programming code.

• Pseudocode, by nature, exists in various forms since there is


no standardization.
ICT172: Principles to Programming
Lecture Notes 7
Advantages of Pseudocode
• Pseudocode is designed to be easily readable and understood
by both technical and non-technical individuals.
• Pseudocode is not tied to a specific programming language,
allowing developers to focus on the logic without worrying
about syntax details.
• Since pseudocode is not bound by syntax rules, it allows for
easy modification and adaptation during the planning phase.
• Pseudocode serves as a valuable communication tool between
stakeholders, such as developers, analysts, and project
managers
• Pseudocode are easily debugged for potential logical errors. Thus
it save time and effort during coding.
ICT172: Principles to Programming
Lecture Notes 8
Disadvantages of Pseudocode
• Pseudocode is not executable, meaning it cannot be
directly translated into a running program.
• Unlike programming languages that have strict syntax
rules, pseudocode lacks standardization. There is no
consistency across different pseudocode documents.
• The process of translating pseudocode into actual code
can be time-consuming, especially if there are significant
differences between the pseudocode and the chosen
programming language.
• It is difficult to represent highly complex or advanced
algorithms accurately using pseudocode.
ICT172: Principles to Programming
Lecture Notes 9
Guidelines in Writing Pseudocode
• Keywords should be written in UPPERCASE (CAPITALS)
• Structural elements come in pairs, e.g. for every BEGIN
there is an END, for every IF there is an ENDIF, etc.
• Underline subprogram names. This means that when
refining the solution to a problem, a word in an algorithm
can be underlined and a subprogram developed. This
feature is to assist the use of the ‘top down’ development
concept.
• Use indentation to show structure in the algorithm.
• Pseudocode should be correctly aligned for correct
understanding of the steps.
• Combine statements to avoid writing lengthy pseudocode.
ICT172: Principles to Programming
Lecture Notes 10
Guidelines in Writing Pseudocode
• Choose descriptive variable names.
• Use standard mathematical notation for operations such as (+, -,
*, /).
• Add comments to explain critical steps or decision points.
• Avoid Ambiguity - Be clear and unambiguous in expressing
each step
• Pseudocode should be concise while still conveying the
essential logic. Avoid unnecessary details that do not contribute
to understanding.
• Test the Pseudocode - Mentally step through the pseudocode
to verify that it achieves the intended result
ICT172: Principles to Programming
Lecture Notes 11
Example of a Poorly Aligned Pseudocode
BEGIN
:
:
:
DISPLAY “Enter your name”
ACCEPT name
DISPLAY “Enter your age”
ACCEPT age
IF age is greater than 18 THEN
DISPLAY “You can have a driving license”
ELSE
DISPLAY “You cannot have a driving license”
:
:
:
END
ICT172: Principles to Programming
Lecture Notes 12
Example of a Well Aligned Pseudocode
BEGIN
:
:
:
DISPLAY “Enter your name”
ACCEPT name
DISPLAY “Enter your age”
ACCEPT age
IF age is greater than 18 THEN
DISPLAY “You can have a driving license”
ELSE
DISPLAY “You cannot have a driving license”
ENDIF
:
:
:
END
ICT172: Principles to Programming
Lecture Notes 13
Lengthy Pseudocode
BEGIN
:
:
:
DISPLAY “Enter first number”
ACCEPT number1
DISPLAY “Enter second number”
ACCEPT number2
DISPLAY “Enter third number”
ACCEPT number3
:
:
:
END
ICT172: Principles to Programming
Lecture Notes 14
Related statements Combined
BEGIN
:
:
:
DISPLAY “Enter three numbers”
ACCEPT number1, number2, number3
:
:
:
END

ICT172: Principles to Programming


Lecture Notes 15
Keywords when writing Pseudocode
• START/BEGIN: To begin the pseudocode.
• INPUT/ACCEPT/GET/ENTER: Take input from the user.
• PRINT/OUTPUT/DISPLAY: To print the result on the screen.
• READ: To read data from the file.
• SET, INIT: Initialize or set a variable to an initial value.
• INCREMENT: Increase the value of the variable
• DECREMENT: Decrease the value of the variable
• COMPUTE, CALCULATE, DETERMINE, FIND: To calculate the result of
an expression.
• STOP/END: To end the pseudocode

ICT172: Principles to Programming


Lecture Notes 16
Example
•Three project assignment will be written for this course
each of which is worth 10%. The course lecturer is
interested in calculating the average of the three
project scores. Write a pseudocode to represent the
algorithm for this problem.

ICT172: Principles to Programming


Lecture Notes 17
Suggested solution
BEGIN
NUMERIC nScore1, nScore2, nScore3, nAverage
DISPLAY “Enter the three project scores:”
ACCEPT nScore1, nScore2, nScore3
nAverage = (nScore1 + nScore2 + nScore2)/3;
DISPLAY “The average of the three scores is ”
DISPLAY nAverage
END

ICT172: Principles to Programming


Lecture Notes 18
Example
•With the increase of petrol prices, your friend, Ahmad
wants a program that will calculate how much 𝑘𝑚
𝑝𝑒𝑟 𝑙𝑖𝑡𝑒𝑟 of petrol is consumed on a trip.

•For example, when starting a journey, he will fill up his


car’s tank to full and reset his car’s trip meter to zero.
After travelling 378 miles, Ahmad stopped at a petrol
station to refuel. The car required 25 liters to fill up the
tank. The program should calculate the number of km
the car can be driven per liter.

ICT172: Principles to Programming


Lecture Notes 19
Suggested solution
BEGIN
NUMERIC nkmDriven, nliterOfPetrolUsed, nkmPerLiter
SET nkmDriven = 378
SET nliterOfPetrolUsed = 25
nKmperliter = nkmDriven / nliterOfPetrolUsed
DISPLAY nkmPerLiter
END

ICT172: Principles to Programming


Lecture Notes 20
Example
•Develop a pseudocode for a program that accepts
the radius of a circle, computes and outputs the area.

ICT172: Principles to Programming


Lecture Notes 21
Suggested solution
BEGIN
NUMERIC nRadius, nArea
SET nPI = 3.142
ACCEPT nRadius
CALCULATE nArea = nRadius*nRadius*nPI
DISPLAY nArea
END

ICT172: Principles to Programming


Lecture Notes 22
Example
•Write a Pseudocode to Find the largest of three
numbers

ICT172: Principles to Programming


Lecture Notes 23
Suggested solution
BEGIN
NUMBER num1,num2, num3
DISPLAY "Enter three numbers"
ACCEPT num1, num2, num3
IF num1>num2 AND num1>num3 THEN
OUTPUT num1, “is highest”
ELSE IF num2 > num1 AND num2>num3 THEN
OUTPUT num2, “is highest”
ELSE
OUTPUT num3, “is highest”
ENDIF

END
ICT172: Principles to Programming
Lecture Notes 24
Example
•Write a Pseudocode check if a number is Positive or
Negative and display an appropriate message.

ICT172: Principles to Programming


Lecture Notes 25
Suggested solution
BEGIN
NUMERIC num
DISPLAY "Enter a number"
ACCEPT num
IF num>0 THEN
OUTPUT "Number Entered is Positive"
ELSE IF num <0 THEN
OUTPUT "Number Entered is Negative"
ELSE
OUTPUT "Number Entered is Zero"
ENDIF

END
ICT172: Principles to Programming
Lecture Notes 26
Example
•The cashier of a super market wants to compute
the amount payable for an item bought from the
supermarket given the quantity bought and the
unit price of the item.

•Develop a pseudocode for the problem’s


solution.

ICT172: Principles to Programming


Lecture Notes 27
Example
•Your course teacher wants to award a bonus mark of
20 to students on top of their exams score during the
semester exams for students who have made the
minimum class attendance in the semester. The
minimum class attendance to receive the bonus mark
is 10 consecutive class attendance. Write the
pseudocode to represent the solution to this problem
which should include displaying bonus due a student.

ICT172: Principles to Programming


Lecture Notes 28
Home Practice
• Write pseudocode that reads two numbers and multiplies
them together and print out their product.
• Write pseudocode that reads in three numbers and writes
them all in sorted order.
• Write pseudocode to calculate Area and Perimeter of
Circle.
• Write a pseudocode that solves the problem of taking
any two integers and performs the following operation:
1. Their sum
2. Their product
ICT172: Principles to Programming
Lecture Notes 29
30

End of Lecture 6

ICT172: Principles to Programming


Lecture Notes

You might also like