SECTION 2:
PROBLEMSOLVING
AND PROGRAM DESIGN
Objective 1 7
PROBLEMSOLVING USING
COMPUTERS
General problemsolving
Every problem requires a solution to resolve
it. Finding the correct solution
to a problem can be considered as problemsolving.
Problemsolving involves identifying,
analyzing and resolving problems using logic,
reasoning skills and analytical skills.
Generally problems can be solved by following
stages:
Stage 1: Definition of the problem
Stage 2: Analyze the problem
Stage 3: Propose and evaluate possible solutions
Stage 4: Select and justify the optimal solutions
Stage 5: Implementation and review
STAGES IN GENERAL PROBLEM SOLVING
Stage 1: Definition of the problem
look at the problem carefully and identify what is the actual
problem, and then write it down (problem statement).
Stage 2: Analyze the problem
look at the problem and identify the inputs, outputs and
processing that is required to complete the task.
Stage 3: Propose and evaluate possible solutions
some problems may have more than one solution, identify the
solutions that are available, and the strength and weakness of
each solution
Stage 4: Select and justify the optimal solutions
Based on strengths and weaknesses, choose the most efficient
suitable (optimal) solution that will solve the problem.
Stage 5: Implementation and review
• optimal solution put into practice, checked to see if it works.
Observe solution and review it to see everything is fine with it.
SOLVING
Breaking down the problem into its significant parts
stages or steps used for general problemsolving have to be
modified therefore the revised steps for computerbased
problemsolving will be:
Stage 1: Definition of the problem
The problem must be looked at carefully and if it is not
phrased properly, you should modify that to make sure
that it is clearly stated.
Stage 2: Analyze the problem
identify the inputs to be used, outputs required, values to be
stored (if any), and the processing that needs to be done to get
the correct outputs.
Inputs will be the instructions that are needed to solve the
problem, processing will be working with the given
instructions, output will be the expected results and storage
will be the values that need to be stored in order to display
the results
IPO CHART OR DEFINING DIAGRAM
An IPO (InputProcessingOutput) diagram can be used to
breakdown the problem. An IPO diagram is a table with
three columns showing the input, output and
processing parts of the problem. Storage is a form of
processing because the computer stores data by using its
processing capabilities.
Example: Get two numbers, add them together and
display their sum.
I P O
COMPUTERBASED PROBLEMSOLVING (CONTD)
Stage 3: Propose and evaluate possible solutions
generally, all problem statements have more than one option. So, you
evaluate each option to see which is most appropriate for you and choose
the best option.
Stage 4: Develop and represent an algorithm
break down the problem into simple manageable steps, so that you can
handle them easily. These simple steps of a problem are known as an
algorithm.
Stage 5: Test and validate the algorithm
check the algorithm you wrote using some values to
ensure that it produces the required results. This process
of desk checking allows you to solve some errors before you convert it
into computer instructions.
Stage 6: Implement the algorithm
here the steps of algorithm are written using a programming language
so that a computer can operate on it. When the instructions are in a
programming language, the instructions are called a program.
COMPUTERBASED PROBLEMSOLVING (CONTD)
Stage 7: Run the program
here you run your program using a programming language to
see if the program is producing the correct results. Note: we
will use any “Pascal compiler” to run our programs
Stage 8: Document and maintain the program
This involves preparing documentation that will help the user
to operate the program and maintain the program by making
changes to the program from time to time, if needed.
CONSTANTS, VARIABLES AND LITERALS
Computers use memory to store data and information.
Each of the data items being inputted has to be stored
in a location in the computer's memory. Memory can
therefore be considered as a as a collection of boxes. Each of
these boxes can hold values, that is, data items.
To represent a box to store a value, it is represented with
a label called an identifier. An identifier is therefore a
named location given by the programmer and can be of two
types, a variable and/or a constant.
Identifiers where the values change throughout the
program are called variables while identifiers where the
values remain the same throughout the program are
called constants. Lets do an example now.
Literals are constants that are written literally as itself
rather than as a value. Examples, 'the sum is', 'largest .' and
'enter number'. They are normally used with the input/output
instructions that appear as a message for the user.
DATA TYPES
Data type determines the type of data that a variable
can store. A data type that treats a variable as a single
unit is called an elementary data type.
A variable can store any of the following data types:
Integers: These are whole numbers, positive or negative
without decimal places, e.g. 5, 45,39, and +126.
Floating point or real numbers: These are positive or negative
numbers with decimal values, e.g. 0.55, 39.2, 5.6 and +6.7.
Characters: A character is anything that you can key in from a
keyboard. This includes letters, numbers and special characters,
e.g. k, L, # and *.
ASSESSMENT
1. List the main stages you should go through when
trying to solve a general problem.
2. Variables and constants are both identifiers. Explain
the difference between them.
3. What is the difference between an integer and a
floating point or real number?
4. Identify the variables/constants in the problem below:
Get a list of food items, their costs and quantities, then
calculate the amount o be paid at the cashier, with a
discount of 5% when quantities are 3 or more. The
problem will then find total cost which should include
a GCT of 16.5% to the customer. Print the details.
PROBLEMSOLVING USING ALGORITHMS
The steps or instructions that can be used to complete a task
are called an algorithm. Note that, you should create an
algorithm before writing a program, not the reverse.
Characteristics of algorithms:
A good algorithm should have the following characteristics:
1. The number of steps must be finite
the computer should has a definite number of instructions to follow
2. The steps must be precise
The instructions or steps must be accurate
3. The steps must be unambiguous
The steps must be very clear so that they can be carried out easily .
An instruction that can be carried out is called an efficient
instruction.
4. The steps must have flow of control from one process to
another.
5. The steps must terminate and
6. The steps must lead to an output.
STEPS FOR DEVELOPING AN ALGORITHM
Any algorithm that is used to solve a problem or task can be
divided into three main steps:
1. The input step –
This is where the instructions from the user are being gathered.
2. The processing step –
This is where the instructions are worked through. It can involve
all or some of the following steps:
a. Assignment In this step the values are assigned to
variables. b Decision This step will be included when you
have to check
for any conditions to be followed.
c. Repetition When you have to repeat a task a specified
number of times then you would include this step.
3 . The output step This step is used to display the results.
ASSESSMENT
1. What is an algorithm?
2. List four characteristics of a good algorithm.
3. What are the three main steps involved in
creating an algorithm?
4. What is meant by assignment?
CONTROL STRUCTURES
The steps that are identified for preparing algorithms
can be written using three basic control structures or
program constructs, namely
1. Sequence
2. Selection
3. Iteration (Repetition)
1. Sequence
The sequence control structure is used when you have
instructions to be carried out in a particular order.
Using: Instruction 1 or Step1
Instruction 2 or Step 2
Etc
Lets look at an example now:
CONTROL STRUCTURES CONTD.
2. Selection
The selection control structure is used in problems with
instructions to be carried out if a certain condition is met.
The choice of options will be dependent on whether the
condition is true or false. Selection control structure
statements commonly used in algorithms are normally
written as:
lf <condition> then <instructions to be performed if the
condition is true> else <instructions to be performed if the
condition is false> . Lets do that now
Step 1: Start
Step 2: Accept score
Step 3: If (score more than 59) then
display ( “the student has Passed”)
else
display ( “student has failed” )
Step 4: Stop
CONTROL STRUCTURES CONTD.
3. Iteration or repetition or loop
Iteration forms the repetitive stage of the processing step.
There are times where we have to repeat a process. Suppose
you want to accept 100 numbers from the user and find their
sum, it would be wise to repeat the process of getting the
numbers 100 times rather than writing instructions 100 times
to accept the umbers. Repetition of a set of instructions a fixed
number of times is called bounded iteration.
Commonly used iteration statements in algorithms
include forendfor.
There are also situations where the process has to be repeated
until a specific condition becomes false. . For example, find the
sum of a set of numbers terminated by O.
Repeating a set of steps a number of times until a particular
condition becomes false is called unbounded iteration. Common
unbounded iteration statements are whileendwhile, and
repeatuntil.
TYPES OF ALGORITHMS
1. PSEUDOCODES
Algorithms are preparation for giving instructions to the computer.
The instructions have to be in a form that is understandable to
computers, so are written using a programming language.
Narrative steps are very simple and useful for when you start
learning about programming, but when you are comfortable with
programming you can create algorithms using instructions with
words and symbols that closely resemble computer programming
language instructions. This form of algorithm representation is
called a pseudocode.
The name 'pseudocode' comes from 'pseudo', meaning 'fake' and 'code'
meaning program. In pseudocode even though the terms used closely
resemble programming language terms, they are used without
following the rigid rules of the language.
Pseudocode language
The pseudocode can contain variables, constants, operators, and
terminology used in programming languages.
PSEUDOCODES CONTD
Variables
Variables are used to store values that can change.
Examples of variable names that can be used in
pseudocode are num1, num2, sum, average, etc.
Constants
Constants are fixed values used when you need to keep a
value fixed. For example, to calculate the area of a triangle
using the formula ½ * base* height, the value 1/2 will be a
constant.
Operators
Operators are symbols used for performing calculations or
making comparisons. Commonly used operators in
pseudocode are:
1 Arithmetic operators operators used to perform mathematical
operations Arithmetic operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
Relational operators operators used for comparisons. The table
below shows commonly used relational operators and their operations
Relational operator Operation
> greater than
< less than
>= greater than or equal to
<= less than or equal to
<> not equal to
= equal to
3. Logical operators - operators used to make comparisons with
multiple criteria.
logical operator Operation
AND And
OR Or
NOT Not
Just as in mathematics, in computing all the operations are carried
out in a hierarchical order. The-computer follows the BODMAS rule.
So anything in Brackets will be done first, followed by Orders (such as
powers and square roots), then Division, Multiplication, Addition and
Subtraction.
Addition and Subtraction will be done last. So when you are writing
instructions you must ensure that you write them in the order in which
you want the computer to carry them out.
Lets do some exercises now.
PSEUDOCODE TERMINOLOGY
The general programming language terms used in pseudocode are:
Terms used for the input step: input, read
Terms used for the output step: output, write, display
Terms used for the assignment step: set, store
Terms used for selection: ifelseendif , case endcase
Terms used for iteration (bounded): forendfor
Terms used for iteration (unbounded): whileendwhile, repeat
until
Practice:
1. Write a pseudocode algorithm to find the average of three
numbers.
2. Prepare a pseudocode algorithm that will accept the length and
width of a rectangle and calculate and display its area.
3. Write a pseudocode algorithm that will accept 20 numbers and
find their product.
TYPES OF ALGORITHMS
1. FLOWCHART
Flowcharts: give a diagrammatically or graphical
representation of a process (algorithm). Flowcharts use the
variables, constants and operators that are used in pseudocode
language, but linked together by different shapes that
represent each type of step.
Symbols used Symbol Function
Start/ Stop
Process
Input/ output
Decision
Flow lines
connector
MEANING OF SYMBOLS
Terminator symbol (oval) used to indicate the
beginning/ending or start/stop of a problem
Process symbol (rectangle) used to indicate the processing
[assignment, calculations, etc.).
Input/output – (parallelogram) used for input e.g. Input X or
Read X, as well as output e.g. Display (“The sum is: ”, sum)
Decision symbol (rhombus or diamond) used in making a
decision between two options yes or no. e.g. sum > 50?
Flow control (arrow) used to show the flow of control of steps
Connector symbol (small circle) connect sections of a
flowchart when a flowchart is long and cannot fit on a page. A
letter or digit can be placed in the small circle to indicate the
link on the first page. Another identically labelled connector
is placed on the next page to indicate the continuation of flow.
So, two connectors with identical labels will serve the purpose
of a long line of flow.
DRAWING FLOWCHARTS
The following steps will guide you in drawing a flowchart.
1. Go through the problem carefully and ensure that all the
information needed to solve the problem are available, such
as the inputs, processing that needs to be done and the
outputs required.
2. Prepare a pseudocodealgorithm so that you can get the
steps.
3. Arrange the steps in a logical order the order in which the
instructions should take place.
4. Draw the flowchart for each step using the correct symbols
or shapes and arrows to indicate the direction of flow.
Now let's draw some flowchart for examples of algorithms
which were done earlier.
Write a pseudocode algorithm and draw a flowchart to find
and display the sum of num1 and num2
ASSESSMENT
Write a pseudocode algorithm and draw a flowchart to
find and display the sum of 10 and 20.
Draw a flowchart that will read three numbers and
find the product of three numbers.
Draw a flowchart that will accept three numbers from
the user and find and display their average.
Draw flowcharts for the following programs:
a. Program to accept the price of an item and calculate its
VAT at 15%. Print the VAT.
b. Program to accept money in US dollars and convert it to its
equivalent local currency. Print both currencies.
Draw a flowchart that will prompt the user to enter two
unequal numbers and find the largest between them.
FLOWCHARTS
Draw flowcharts