You are on page 1of 30

Problem Solving

Most people regard problems as something


negative, but a problem can be a matter or
situation that could be better, that could be
improved on or that requires a solution.
Steps to solve a problem
1. Identify and define the problem.
2. Analyse the problem and break it into
components.
3. Develop an algorithm.
4. Test the algorithm to see if it works.
5. Write a program in a programming language.
6. Test and debug.

Identify and define the problem

For example, you are experiencing a lot of


headaches. Your immediate solution is to take
aspirin, which eases the pain for a few hours,
but the headaches often return. The problem is
not the headaches. The problem is that
something is causing the headaches. Once you
have realized this, you can identify the root of
the problem.
Once you have been able to identify the
problem, you should define the problem clearly
to ensure there is no uncertainty or ambiguity.
Write the problem clearly with as many
important details as possible.

SBA POINTER: Carefully identify and analyse


the problem presented to you in the SBA
Exercise
Do you think these problem statements are clear
and complete? If not, show how to improve them.
The most important information at this stage is
what has to be calculated and how to calculate it.
1. Convert a weight from kilograms to grams.
2. Calculate the average temperature.
3. Calculate the discount.
4. Add the heights of three students and find the
average.
5. Find the area of the shape.

Analysing the problem

Analysing the problem helps you take a


complex problem and break it into easily
manageable components.
One good approach is top-down design.
This analyses the problem and breaks it into
smaller problems, or sub problems. You then
solve each sub problem and combine the
solutions to solve the original problem.
Stepwise refinement is a more precise
way to do top-down design. This breaks a
problem into smaller problems, then those
smaller problems are broken into even
smaller problems.

Exercise
Think how you would solve some of these real-life problems
for IT professional. You may not have the technical knowledge
to create the solution, but you can think of ways to break
down the problems.
1. A software company that specializes in accounting software
wants to create a range of general-purpose software for
home users.
2. A software developer is taking a long time to fix a piece of
faulty code, because she is not very familiar with the
programming language.
3. A software developer wants to write a program that provides
a range of statistics for data entered about cricket matches.
He wants these statistics to reflect averages, total scores,
run rates and many other figures.
4. A principal is considering a software application for inventory
in a school cafeteria. It should store records of all stocks and
warn when stocks are low.

Determine the input, process and output

IPO chart
You are given a problem of finding the sum of three
numbers.
- The input of the three numbers
- Add the three numbers
- Output
the three numbers
Inputthe sum of
Process
Output
Number 1
Number 2
Number 3

Read three
numbers
Add the three
numbers
together
Output the total
of the numbers

Total

Exercise
Create an IPO chart that shows the input,
processing and output statements of the
following:
Calculate the discount given to customers based
on the value of their purchases. Add the prices of
a customers items (maximum of 3 items) to get
a total. If the total is under $100, the customer
gets no discount. If the total is over $100, the
customer gets a 5% discount. If the total is over
$150, the customer gets a 10% discount. For any
amount over $200, the customer gets a 15%
discount. Show the amount of the discount and
the discounted price on the receipt.

Algorithms
An algorithm is a set of precise
instruction steps to solve a given
problem.
Algorithms can be written as a
narrative, or they can be written in
pseudocode.
They can also be represented on
flowcharts.
NOTE:
The assignment statement in an

Properties of an algorithm
An algorithm must:
1.
2.
3.
4.
5.

Be precise
Be unambiguous
Give the correct solution in all cases
Flow logically
Eventually end

Categories of data
Just as we need containers to hold or store ingredients
for a recipe, likewise, when we perform computations,
we need something to store or hold the values that we
manipulate. In the computer , values are stored in
memory locations. In order to keep track of where our
values are stored, we need to place an identifier or a
label on a particular memory location. The label or
identifier is called a variable.
Therefore, a variable is a symbolic name assigned to a
memory location that stores a particular value.
NOTE: The word variable is derived from the verb to
vary. This means that the value stored in a particular
location can change from time to time, although the
label remains the same.

Constants A constant is data that has a


known value throughout a program. A
constant does not change its value. A
constant can also be a non-numerical
value such as your name or address.
For example, to calculate the area of a
circle you use the formula r2 . You do
not know what the radius is, but the
value of is a constant, about 3.14.
This value will not change and does not
depend on the size of the circle.

Exercise
Which of these values are constants and
which are variables?
1. Your shoe size
2. The amount of a deposit into your bank
account
3. The length of a room in metres
4. The number of months in a year
5. The year you were born
6. The mark received in a test

Datatypes
To write an algorithm, you must be able to distinguish between the different
datatypes because when you write a program, the computer needs to know
what type of data it is working with.
Integers Whole number both positive and negative. They can not represent
fractions or have any decimal points. For example, if the problem you are
solving requires that you enter the number of eggs you bought at a shop, this
is most likely to be an integer because you would not buy a fraction of an egg.
Real number Numbers that include decimal points both positive and negative.
They are sometimes called floating point numbers. For example, monetary
values are almost always represented as real numbers.
Characters A program can output messages such as Pass and Fail, and
input values can consist of characters or symbols. Characters are single letters
of the alphabet or symbols. The letter a is a character and the symbol ! is a
character.
String A string is a group of characters. A string can be any number of
characters. For example, Lucy Wells is a string

Algorithm Structure
Header: Algorithms name or title.
Declaration: A brief description of
algorithm and
variables used.
Body: Sequence of steps.
Terminator: An end statement.

Example of an algorithm
Algorithm Welcome {Header}
This algorithm displays a welcome message
to the user on the screen. {Declaration}
Display Enter first name:
Accept first name
Display Hello, first name
Display Have a nice day!
Stop {Terminator}

Body

Control Structures
A control structure can be a sequence,
selection or a loop (that is, repetition).
The body of the algorithm is
comprised of various structures.
Sequential structures are:
Input statements, for example:
Get num1, num2
Read price, tax-rate
Accept guess
Input num

Output Statements, for example:


Print total-cost
Display average
Output sum
Statements involving arithmetic operations,
such as:
Sum
num1+ num2
Average
sum /2
Statements that assign values to variables,
such as:
Count
0
Maximum
20

Selection structures are:


IF or IF-then-else statements.
They allow decisions to be made, based on some
condition that evaluates to true.
In the case of if-then-else, alternatives are
executed if the condition is false.
Examples
If (A>B)then
Display A
If (age >=50)
Print Old
Else
Print Young

Loop structures that allow statements


to be repeated a fixed number of
times or until some condition
evaluates to false. If the number of
repetitions is known beforehand, the
loop structure is called a counted
loop.
Example
Repeat 10 times
Print I am good looking
End-repeat

If the exact number of repetitions is


unknown beforehand and is based
upon some condition, then the loop is
called a conditional loop.
Example
While (price <> 0) do
Read price
Total
total + price
End-while

Flowcharts

A flowchart is a pictorial representation of an


algorithm. Flowcharts use special geometrical
objects to designate the basic steps of a program.
A parallelogram is used to represent the input
operation as well as the output operation.
A rectangle is used to represent a
processing/assignment statement.
A diamond is used to represent a decision (ifthen-else) structure.
An elliptical shape is used to represent the
terminal indicators, START or STOP.
Directional arrows are used to indicate the flow of
the logic in the algorithm.

Basic shapes used in flowcharts


Start/stop

Decision

Input/Output

Process

Flow of algorithm

Control structures as depicted in a flowchart

Sequence
A

do
B B

do A

Selection (Decision)

No

D
If C is true then
do E
Else
Do D

Yes

Loop (Repetition)

F
Yes
No
While F is true
do G

Algorithm Average
This algorithm finds the average of three
numbers
Start
Read num1, num2, num3
Average
(num1 + num2 + num3) /
3
Print average
Stop.

Flowchart version of the Average


Algorithm
Start

Read num1,
num2, num3

Average = (num1 + num2 +


num3) / 3

Print Average

Stop

Arithmetic Operators

Operator

Meaning

Addition

Subtraction

Multiplication

Division

Relational Operators
Operator

Meaning

<

Less than

>

Greater than

Equal to

<=

Less than or equal to

>=

Greater than or equal to

<>

Not equal

Conditional and looping


operators
Operator

Purpose

IF

Compares a statement against a


condition to see if it is true or
false

THEN

Executes an instruction when a


condition is true

AND

Links two or more conditions that


have to be met

OR

Provides an extra condition

ELSE

Executes an instruction should a


condition be false

FOR

Creates a loop that is carried out


a known number of times

WHILE

Creates a loop that is carried out


an unknown number of times

You might also like