You are on page 1of 46

Problem

Solving
Presented
by Sheryl Robinson

Solving a problem on a
computer involves the
1. Definition of the problem
following
activities:
2. Analyze
the problem.
3. Propose and evaluate possible
4.
5.
6.
7.
8.

solutions
Develop an algorithm (a method) for
solving the problem.
Test and validate the algorithm.
Implement the algorithm( ie Writing
the program for the algorithm)
Run the program
Document and maintain the program.

Definition of the
problem
One has to look
carefully and identify

exactly what the problem is and write it


down. This is called the PROBLEM
STATEMENT.

Analyze the
In this stepproblem.
you would :
identify the inputs to be used or

required (INPUT)
Identify the processing required to
achieve correct output (PROCESS)
Identify values to be stored if any
(STORAGE)
Identify required outputs (OUTPUT)

Analyze the
Problem
This step helps
you to take a complex

problem and break it into smaller or


easily manageable components. It is the
most important step in problem solving

Narrative
A narrative is a representation of an
algorithm Algorithm
where each instruction is written in
everyday language
Problem statement: see question#3
Step1: Start
Step2: Get the two numbers
Step3: Add the numbers
Step4: Store the results in Sum
Step5: Display sum
Step6: Stop

IPO Chart
Input

Processing

Output

A,B

Sum = A+ B

sum

Propose and
evaluate possible
This step involves examining all the
solutions
possible solutions
to the problem and
then select the best one ,given the
resources available (e.g.. time)

Develop an
An algorithm
is a finite number of accurate,
algorithm
unambiguous steps that solve a problem

There are many different methods used for


writing algorithms.
Examples of algorithms are: narrative,
flowcharts, pseudocode, design structures,
top down design approach, bottom up
design approach, etc.
(We will focus on Narrative, Flowchart and
Pseudocode. )

Characteristics of a
good
algorithm
The number of steps must be finite
The steps must be precise
The steps must be unambiguous
The steps must have flow control from
one process to another
The steps must terminate
The steps must lead to an output

Steps for
Developing
an
The input step
algorithm
The processing
step:Assignment
Decision
Iteration (Loop)
Arrays
The output step

Control structure
Control structures are instructions or

statements that determine the flow of


control of steps in an algorithm.
There are three basic control structures:
1. Sequence
2.Selection
3.Iteration (repetition)

Control structures
Sequence
Selection ( if-then, if-then-else)
Iteration (while loop, repeat-until loop
and for loop)

Sequence
The sequence control structure is used when
you have instructions to be carried out in a
specific or particular order.
Example:Start
Read a, b
Sum = a + b
Print Sum
Stop

Selection
The selection control structure is used in

problems with instructions to be carried out


if a certain condition is met. The choice of
the options will be dependent on whether the
condition is true or false.
If < condition> then
true result
Else
False result
endif

Example of
selection

Start
Read Score
If score>=75 then
Print Congrats you have passed
Else
Print "Sorry you have failed
Endif
Stop

Iteration
Iterations or loops are statements or
instructions that get repeated. There are two
types of Iterations:Bounded Repetition of a set of instructions a
fixed number of times. E.g. for-endfor
Unbounded Repetition of a set of
instructions a number of times until a
particular condition becomes false. E.g. Whileendwhile and repeat-until

Example of
iteration-Bounded
To accept 100 numbers and calculate their
sum.
Start
Sum = 0
For I = 1 to 100 do
Read x
sum = sum + x
Endfor
Stop

Example of
iteration-Unbounded
Find the sum of a set of numbers terminated by zero
Start
Declare sum, a
sum = 0
Read a
While a <> 0 do
Sum = sum + a
read a
Endwhile
Stop

Variables, Constants
and
literals
Each storage location in memory is
represented with a label called an
identifier
There are two kinds of identifiers:1. variables
2. Constants

Variables and
constants
A variable
identifies a memory location

in which the item of data can be


changed. E.G. num2, x, y salary
A constant identifies a memory location
where a fixed item of data is stored i.e.
the item of data does not change.
E.G. Week = 7 , pi = 3.14

literals
Literals are constants that are written

literally as itself rather than a value.


Examples:- The average is, The
smallest number is, The highest grade
is
Literals are normally used with the
input/output instructions that appear as
a message for the user.

Data types
Data type indicates or tells the type of data a
variable can store
A variable can store the following data type:1. Integers( these include whole
numbers, positive or negative
numbers without decimal places
e.g. 230, -67, 0, -78

Data types
(contd)
2. Floating point
or Real (these are

positive or negative numbers with


decimal places e.g. 0.75 or -767.84

3. Characters:- are single letters of the


alphabet or symbol. E.g. a, c, k,*,#,$
4. String :- is a group of characters. A
string can be any number of characters
e.g. I love you

Data type Activity


Information Example
Your name
Value of pi
# of weeks
in a year
Temp.
32
outside
# of boys
28

Data type

Variable or
constant

Arithmetic
Operation Operators
Arithmetic operator
Addition

Subtraction

Multiplication

Division

Exponentiation (power) ^ (caret)

Arithmetic
Operation Operators
Arithmetic operator
Finding the reminder in MOD eg 9 mod 2 = 1
a division operation
Integer division

DIV eg 11 div 4 =3

Relational
Operation Operators
Relational Operator
Greater than

>

Less than

<

Greater than or equal to >=


Less than or equal to

<=

Equal

Not equal to

<>

Logical Operators
Operation

Operator

And

AND

Or

OR

Not

NOT

TRUTH TABLES- FOR


AND
A

A and B A

A and B

TRUE

TRUE

TRUE

TRUE

FALSE FALSE

FALSE

FALSE FALSE FALSE

FALSE TRUE

TRUTH TABLES- FOR


OR
A

A OR B A

TRUE

TRUE

TRUE

FALSE

FALSE TRUE

FALSE FALSE

A OR B

TRUTH TABLES- FOR


A
OR
(NOT
B)
A
B
NOT B
1

FLOW CHART
Terminator indicates start or stop
Process indicates processing
Input/output indicates the input or output
of the problem
Decision used to make a decision between
two options ie yes or no
Flow control shows the flow of data
Connector used to connect part of a
flowchart that cannot fit on the page

Testing Algorithm
One good way of testing an algorithm is

to use trace table


A trace table is a table that tests an
algorithm for logical errors by
calculating each of the variables in your
algorithm one statement at a time.

Trace table Activity


-Question 34(a)
M

14

-4

Top-down Design
Top-down design approach analyses the

problem and breaks it into smaller


manageable problems or sub-problems.
Each of the sub-problem is solved
separately and then combine the various
solutions to solve the original problem.
This approach makes it easier to solve
complex problems.

Top-Down design
Question: The students from Grade10G

wants to create an accounting software to sell


to accounting firms in Jamaica
Solution
Research the requirements of the program
Design the algorithm for the program
Development the program into a programming
language
Integrate the different parts
Test the program to ensure that it works

Break the problem


into smaller parts
Create an accounting
software

1. Research

2.Design the
Algorithm

3. Develop the
program

4. Integrate

5. Test the Program

Stepwise
refinement

Stepwise refinement is a more precise way to do topdown design. This breaks down a sub-problem into
smaller problems, then those smaller problems are
broken into smaller problems.
For example the Research the requirements of the
program can be broken down into smaller problems : One group can investigate the requirements of the
Sales Dept (Accounts Receivable).
Another group can investigate the requirements of
the Purchasing Dept (Accounts Payables).

Arrays
An array is a variable that can store a number

of elements of the same data type.


Example of an array called fruits with size of 6
and data type string. The size of an array
determines the number of elements it can hold
item1

item2

item3

orange grapes kiwi

item4

item5

item6

apple

pear

mango

Declaring an array
Var <array name>: array [range] of <data
type>;
E.g. Var
fruits: array[16] of string;
Activity: Declare the following arrays
Variable name
salary
Studentname

Data type
real
string

Array size
20
10

Writing to an
array
Start

Score: array[1..10] of integer


For i = 1 to 10 do
Print please enter student score
read (score [i])
Endfor
Stop

Reading from an
array
Start
Score: array[1..10] of integer
For i = 1 to 10 do
Write the student score is
Writeln (score [i])
Endfor
Stop

Traversing an
array

Start
Score: array[1..10]
For i = 1 to 10 do
Print please enter student score
read (score [i])
Endfor
For i = 1 to 10 do
score [i] =score [i]* 3
Writeln (score [i])
Endfor
Stop

Searching an array
linear search
For i = 1 to 10 do
if score [i] = 95 then
Writeln (Score found)
Else
Writeln (Score not found)
endif
Endfor

The End
Thank you!!!!!

You might also like