PROBLEM SOLVING
AND
PROGRAM DESIGN
Specific Objective:
Objectives of the Lesson
► At the end of the lesson, students should be able to:
► Outline the steps in problem solving
► Use a divide-and-conquer approach to decompose
large everyday problems into smaller tasks.
► Define a problem by decomposing it into significant
components.
► Distinguish between variables and constants
► Explain the concept of algorithm
► Represent algorithms in the form of pseudocode and
flowcharts.
► Test algorithms for correctness.
Objectives of the
lesson
Objective1.1: Outline the steps in problem
solving
Objective1.2: Use a divide-and-conquer
approach to decompose large everyday
problems into smaller tasks.
Objective1.3: Define a problem by
decomposing it into significant components.
Key terms in Problem
Solving
► Problem-solving is the process of
creating a set of instructions that,
when executed, accepts input data
and produces meaning information.
► Solution- A solution is a set of
instruction that, if followed in order,
will produce the required
information.
Key terms in Problem Solving
► Algorithm – An algorithm is a
sequence of instructions which
rigorously defines a solution to
a problem.
► Put more simply, an algorithm is
a set of instructions, written in
everyday English, that solves a
problem.
Key terms in Problem Solving
Two types of Algorithms are:
► Pseudocode- A pseudocode is a
language consisting of English-like
statements used to define an algorithm.
Pseudocode is a formal way of writing
an algorithm using structured English
text, numbers and special characters.
► Flowchart- A flowchart is a pictorial
way of representing an algorithm using
a set of standard symbols (shapes).
Steps in Problem Solving
► Define the problem
► Propose and evaluate solutions
► Determine the best solution
► Represent the solution as a
algorithm as pseudocode or a
flowchart.
► Test and validate the solution
Steps in Problem Solving
Steps Outline of Activities
Define the problem Aim is to understand the problem.
Start with a clear description of the
problem in narrative form. An Input-
Process-Output (I-P-O) chart is
used to beak the problem down into
three main component parts.
Propose and evaluate solutions Consider alternative solutions to the
problem. Evaluate each proposed
solution.
Determine the best solution Based on the evaluation of the
solutions from the previous step,
determine which one is best.
Steps in Problem Solving
Steps Outline of activities
Represent the algorithm as Represent the solution as
pseudocode or a flowchart formal pseudocode or a
flowchart.
Test and validate the solution Test the pseudocode/flowchart
for correctness using a trace
table. If the algorithm produces
correct results for a set of
carefully selected test data
then the design is deemed valid
and program implementation
may begin.
Defining the Problem
► Defining the problem is a way to help the
programmer understands what he or she is
required to do. It involves breaking down the
problem into three (3) key components:
1. What is given (the input)
2. The expected results (the output)
3. The tasks that must be performed (processing)
⮚ These three (3) components can be illustrated
using what is called a defining diagram.
⮚ A defining diagram is a table with three(3)
columns, that shows the Input Processing and
Output steps and is often referred to as an IPO
chart.
The defining diagram is a formal
approach to defining a problem. which
represent the three(3) components:
Input, processing and output.
Input Processing Output
► The Input
The input is the source data provided. You can
identify what is the input in a given problem by the
following keywords: GIVEN, GET, READ OR
ACCEPT.
The Output
The output is the end result required. You can
identify what is the output in a given problem by the
following keywords: PRINT, DISPLAY OR OUTPUT.
The Processing
The processing column is a list of what actions are to
be performed to achieve the required output. In this
we write the functions that we will be carrying out on
values. An example here would be, we are asked to
get two values, sum them and divide the result by 3.
Having received the values we could write:
Sum = (val1 + val2)/3 etc.
Defining Diagram
Examples
Here are some examples we could use
and place the steps in the IPO chart,
lets discuss each.:
1. Real life scenario: Baking a cake
2. A program is required to read two
numbers, calculate and print their
difference.
3. A program is required to read three
numbers, calculate and print their
products.
Practice Questions
► Use a defining diagram to answer the
following:
1. A program is required to read the unit cost
and quantity of an item. Calculate and
print the total cost of the item.
2. A program is required to accept a number
and to find the square of the number. The
program should also output the square of
the number.
3. Read the regular price of an item; compute
discount amount at 20% of regular price
and the item’s discounted price: Print
discount amount and the discount price.
Practice Questions
► Use a defining diagram to answer
the following:
1. Input the minimum and maximum
temperature reading. Calculate and
output the average temperature.
2. Read a number, add 10% and output
the result.
3. Find the final mark of a course. The
final mark consists of a course work
mark and an exam mark.
MORE ON
ALGORITHMS
Objective 1.4: Distinguish between
variables and constants
Objective 1.5: Explain the concept of
algorithm
Variable
► A variable is a named location in
memory that stores a particular value.
This value may be changed during the
execution of a program. Examples of
variables names include: ‘number’,
‘age’, ‘name’, ‘length’, ‘the area of
the circle’ and so on.
► N.B. You can have several different
variable names, numbers, ages, etc.
For additional resource materials:
https://data-flair.training/blogs/python-variables-and-data-types/
Constant
► A constant provides locations for storing
data which do not change value during
the execution of a program. For example
if you are calculating the total price of
an item which included general
consumption tax (GCT), which is, for
example, 16.5% , the 16.5% would be
treated as a constant value in the
calculation.
Some other examples are:
► The number of months in a year
► The value of pi as a fraction 22/7
Data Types used in
Programming
► Variables typically store values of a given
type. There are several data types, integer
(int), floating points or real, characters
(Char), string (str) and boolean (bool).
► Integer- used to store whole numbers
(whether negative or positive) for example,
5, -10, 20 etc.
► Floating point (real)- used to store
fractional numbers and numbers with a
decimal point, for example 3.1, 22/7,
$12.50 , -575, 350.0 etc.
► Characters (Char)- any single character in
the ASCII set (a single character such as a
letter of the alphabet or punctuation) for
example, ‘A’, @, $ etc.
Data Types used in
Programming Cont’d
► String- a collection of characters such as a word,
phrase or sentence, for example, Natalee.
► Boolean- used to store only two possible
values. Eg. True or False etc.
► Literals- are special class of data type, and they
cover those values that are fixed (even if only
temporary). For example, consider the following
output statement:
Print “Please enter your name”. The words
shown in quotation would be considered an
example of a literal string value.
The following table gives some
examples of the various data
types.
Data Type
The grade a student got in a course, eg. A, B, Character
C, D, E etc.
The time Usain Bolt takes to run a 100m race Real Number
(in seconds). Eg. 9.58 sec
The number of books in a person’s bag. Eg. 5 Integer
Books
Whether or not a piece of luggage is Boolean
overweight. Eg. Y or N
The cost of an item in a store. Eg. 135.50 Real Number
A person’s name. Eg. ‘Michelle Obama’ String
Algorithm
► An algorithm is a sequence of precise
instructions or steps used for solving a
problem in a finite amount of time.
Characteristics of an Algorithm
1. It must be precise (exact)
2. It must be unambiguous (in other words
it must be clear).
3. It must give the correct solution in all
cases.
4. An algorithm must terminate or
eventually end.
Video
Copy and paste the link below to watch the video or
click the link in presentation view
► Video: Computer Science Basics: Algorithms
https://www.youtube.com/watch?v=kM9ASKAni_s
Pseudocodes
Objective 1.6: Represent algorithms
in the form of pseudocode.
video
► Watch a video that introduces psuedocode. What
are the three structures used in pseudocode?
► Video: 5 Minutes to Code: Programming Basics
“Pseudocode”
► https://www.youtube.com/watch?v=HhBrkpTqzqg
What is Pseudo-code?
► A pseudo-code is an imitation of a
computer written using mathematical
notations and English-like statements
to describe the logics to solve a
problem or carry out a procedure.
The pseudo-code is used as a guide
to code the solution to the problem in
a high-level language such as Pascal
Programming.
Parts of a Pseudo-code
Algorithm
A pseudo-code contains one or more
of the following statements:
1. Input Statement
2. Assignment Statement
3. Output Statement
4. Control structure (sequence,
selection, repetition).
Input Statements
► The input statement is used to get data
outside the computer from a particular input
device into a variable for manipulation by the
pseudo-code. The key words we will use are
‘input’ or ‘read’. To read data into a
variable, you write the word ‘Input’ or
‘Read’ followed by the variable name
► Examples:
1. READ Score
2. READ Fname
3. READ Lname
4. READ Age
Output Statements
► The output statement is used to get information to
the programmer or to the user.
► In pseudocode we use the keywords PRINT,
DISPLAY AND OUTPUT “ ” to achieve the
output. They are used to output strings, variable
values or a combination of both.
► PRINT “Meaningful statement”
► PRINT “Meaningful statement” (variable)
Pseudocode User sees
PRINT “Today is a good day!” Today is a good day!
PRINT “Your name is” (name) Your name is Johnny
PRINT “Enter your age: ” Enter your age:
Prompt Statement
► We also have a statement called the prompt
statement. A prompt statement is actually an output
statement which displays on the screen, to the user, a
message indicating what actions to take based on the
program written.
► For example, you may be asked to write a pseudo-
code to accept two numbers and prompt the user to
enter the numbers (this would be done via the
keyboard).
Print “Please enter two numbers”
The prompt
Output
Key Read num1,statement
num2
word written in
open and
close
quotations
Assignment Statements
► An assignment statement is used to store a
value in a variable. The assignment statement
has two parts, the Left value and the Right
value.
► An assignment statement is written as follows:
left value 🡨 right value
► The right value is a literal or an expression that
evaluates to a value that will be stored in the left
hand value which is a variable.
► Examples:
► area 🡨 pi*radius*radius
► Product num1 * num2
Pseudocode Program
Structure
Algorithm Name:
Purpose of Algorithm:
Declaration of constants
Declaration of variables
START
program statements
STOP
Declaration of Constants
and Variables
Recall: Variables and Constants MUST
have meaningful names.
► Constants are always declared before
variables.
Declaration of Constants and Variables
Structure
SET constant TO value
DECLARE variable1, variable2 as data
type
DECLARE variable3, variable4 as data
type
Example 1
SET pi TO 3.14
DECLARE radius, area as
real DECLARE count,
sum as integer
Example 2
SET discp TO 0.25
DECLARE fname, lname as
string DECLARE code as
character
Class Examples
1. Write an instruction to read the names of four
students.
2. Write an instruction to enter the birth date,
height, weight and complexion of a student.
3. Write an instruction that prompt the user to enter
his or her name, store it and output the name.
4. Write an instruction that prompt the user the
enter two scores and output it.
5. Write a structured pseudocode which requests the
user to input the price of an item. The program
should also allow the user to enter the price and
output it with a suitable label.
Class Exercise
1. Write a structured pseudo-code to find the square
of a number. Output the square of the number.
2. Write a structured pseudo-code to read the unit
cost and quantity of an item. Calculate and print
the total cost of the item.
3. Write a structured pseudo-code to read two
numbers, calculate and print their difference.
4. Write a structured pseudo-code to read three
numbers, calculate and print their average.
5. Write a structured pseudo-code that prompt the
user to input the ages of four of your friends. The
program should allow the user to input these
ages, find their average and print it with a
suitable label.
FLOWCHARTS
Objective1.7: Represent algorithms
in the form of flowcharts.
Flow Charts
► Watch a video that introduces flowcharts. What is
a rhombus shape used for in a flowchart?
► Which is the only flowchart symbol which MUST
be used at least twice in every flowchart?
► What is the purpose of flow lines in a flowchart
What is a Flowchart?
► A flow chart is a graphical representing an
algorithm using a set of standard symbols
(shapes).
► Flowcharts use different symbols to
represent input, processing and output
operations. Operations are connected with
arrows which serve as flow lines.
Flow Charts
► 1. A parallelogram
Input/Output Symbol: This symbol indicates inputs
to and outputs from a process. The shape must be
labelled with either INPUT or OUTPUT.
Input Output
Price Price
► 2. A rectangle
Process Symbol: Program instruction(s) that transform(s)
input(s) into output(s) are recorded here.
Each symbol MUST be labelled with an operation that
includes the assignment operator ( or =).
Sum = num1 +
num2
Example:
► A Diamond
► Decision Symbol: Indicates a question or branch
in the process flow; for example x > 0?
► No need to write the words ‘IF… THEN’ in the
shape. Label one branch Yes and the other NO.
Num1
Is
>
count
Num2
= 10?
?
► 4. Terminator: Terminators show the start and stop
points in a process. Use only TWO terminators for EACH
flowchart. Label the first terminator BEGIN and the
second one END.
Start Stop
Flow lines
► 5. Flow lines: join flowchart symbols and
indicates the direction in which the process
flows.
Example of Flow Chart
Relational Operators
► The relational operators are used for comparison
of the value of one element with another. There
are six types of relational operations: equal,
greater than, less than, greater than or equal to,
less than or equal to, and not equal to. Each of
these operations can be used to compare the
values of the variables.
► The result of each of these operators is either true
or false. When using these operators, make sure
all the arguments are the same data type.
Integers should be compared with integers,
strings with strings, and so on.
Operator Names Symbol Description
Equal = Return true if both sides
are equal.
Greater Than > Returns true if the
variable on the left is
greater than the variable
on the right.
Less Than < Returns true if the
variable on the left is
less than the variable on
the right.
Greater than or >= Return true if the
equal to variable on the left is
greater than or equal to
the value of the variable
on the right.
Less than or <= Return true if the
equal to variable on the left is
less than or equal to the
value on the right.
Not equal to <> Return true if both sides
are not equal.
PASCAL PROGRAMMING
► PLEASE USE THE LINK BELOW TO ACCESS THE
TUTORIAL ON PASCAL.
► TO THE LEFT OF THE WEBPAGE IS THE MENU.
CLICK ON EACH OPTION TO VIEW EXAMPLES OF
HOW THE CODES ARE TO BE DONE
► TUTORIAL LINK BELOW (COPY AND PASTE THE
LINK TO THE URL AND PRESS ENTER:
https://www.tutorialspoint.com/pascal/pascal_progra
m_structure.htm