You are on page 1of 46

Problem Solving

Footer Text 10/30/2015 1


Learning Outcome
• List five (5) steps in problem solving 8.2.1
• Identify input, process and output from a given
problem. 8.2.2
• Define algorithm. 8.2.3.1
• Solve a given problem using algorithm. 8.2.3.1
• Explain the purpose of each control structure. 8.2.3.2
• Apply appropriate control structure in problem
solving. 8.2.3.2

Footer Text 10/30/2015 2


Steps in Problem Solving
• There are five (5) steps in problem solving.

Problem Design a
Implementation Testing Documentation
Analysis Solution

Footer Text 10/30/2015 3


Steps in Problem Solving
•Be sure you understand what the program should do,
•What the output should be. IPO
Problem •Have a clear idea of what data (or input) are given and the
Analysis •Relationship between the input and the desired output.

•Find a logical sequence of precise steps that solve the problem Algorithm
Design a •Such a sequence of steps is called an algorithm •Pseudocode
•Every detail, including obvious steps, should appear in the algorithm
Solution •Flow chart

•Translate the algorithm into a programming language


•Coding is the technical word for writing the program. C++ Coding
Implement

Footer Text 10/30/2015 4


Steps in Problem Solving
•Locate and remove any errors in the program Output /
•Testing is the process of finding errors in a program, and result
debugging is the process of correcting errors that are found
Testing •An error in a program is called a bug

•Organize all the material that describes the program


•intended to allow another person or the programmer at a
later date, to understand the program
•consist of a detailed description of what the program does
Documentation and how to use the program

Footer Text 10/30/2015 5


Error in the program
 Syntax error :
Program error that occurs when the code violates the syntax
or grammar of the programming language.
e.g : misspelling a command, leaving out require command.
 Logic error :
Flow in program design that causes inaccurate results.
e.g : sum = no1- no2
 Runtime error :
Program error or event that causes the program to stop
running.
e.g : error when using the internet

Footer Text 10/30/2015 6


8.2.2 Problem Analysis
• Input-Process-Output (IPO) Analysis
o to analyze problems and develop algorithms
o to organize and summarize the results of a problem analysis
o to shows where in the solution the processing takes place
o It can also be represent using IPO chart/table.

Footer Text 10/30/2015 7


8.2.2 Problem Analysis with

Input-Process-Output (IPO) Chart


Input Processing Output

Read Data Perform Display


An item – an Computation Results
input/data that is an intermediate value Print result/s on
needed by that the algorithm uses screen, on paper
computer. when processing the etc.
input into the output

Footer Text 10/30/2015 8


8.2.2 Problem Analysis with
Input-Process-Output (IPO) Chart
Examples 1 How to make a tea drink

1. Put water into a


Sugar cup Tea drink
Water 2. Add tea bag and
Tea bag sugar into a cup
3. Stir

Footer Text 10/30/2015 9


8.2.2 Problem Analysis with
Input-Process-Output (IPO) Chart
Examples 6

input process output

Footer Text 10/30/2015 10


8.2.2 Problem Analysis with
Input-Process-Output (IPO) Chart
Math’s Problem
Example 1

Problem statement: calculate total of two numbers

Problem Analysis:

no1 Calculate total total


no2 total = no1+ no2

Footer Text 10/30/2015 11


8.2.2 Problem Analysis with
Input-Process-Output (IPO) Chart
Math’s Problem
Example 2

Problem statement: Calculate the area of a rectangle

Problem Analysis:

width Calculate area of rectangle area of rectangle


area = width x height
height

Footer Text 10/30/2015 12


8.2.3.1 Algorithm
• step-by-step instructions that will transform the input
into the output
• can be represent using 2 methods;
o pseudocode OR
o flow chart

Footer Text 10/30/2015 13


8.2.3.1 Algorithm
Pseudocode Flow Chart
• a graphical representation of a
• an artificial, informal language algorithm in relation to its sequence
(similar to English) used to develop of functions
algorithms
• special-purpose symbols connected
• a notation resembling a simplified by arrows (flow lines)
programming language, used in
program design

Footer Text 10/30/2015 14


8.2.3.1 Algorithm
Solve a problem using an Algorithm
Conclusion Flow Chart symbols and Meaning

Flow line

Terminal

Input/output

Process

Decision

Footer Text 10/30/2015 15


8.2.3.1 Algorithm
Solve a problem using an Algorithm

• General’s Problem
o Feeling thirsty
o Withdrawing money from an ATM machine.

• Mathematical Problem
o Finding an area of shape (rectangle, triangle)
o Mean for marks

Footer Text 10/30/2015 16


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• General’s Problem  Feeling Thirsty
o Do Problem Analysis (prepare an IPO)

Input Processing Output

Water Drinking Happy Face !


•Buy any drinking water •Open the cap and start drinking.
from shop or a vending •Repeat the process until satisfy.
machine, OR
•Get it from friend, OR
•…

Footer Text 10/30/2015 17


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• General’s Problem
 Feeling thirsty
o Examples of
START
Pseudocode WALK TO NEAREST SHOP
• a notation BUY A DRINK
resembling a DRINK
simplified HAPPY FACE
programming END
language, used
in program
design

Footer Text 10/30/2015 18


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• General Problem  START

Feeling thirsty
WALK
o Examples of Flow TO THE NEAREST SHOP

Chart
• a graphical
BUY
A DRINK
representation of
algorithm in DRINK
relation to its
sequence of HAPPY FACE

functions
END

Footer Text 10/30/2015 19


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• General’s Problem  Withdraw Money from ATM
o Do Problem Analysis (prepare an IPO)

Input Processing Output


• Auto-teller machine • Insert ATM card on the intake • Money
(ATM) card. slot. • Transaction Slip
• PIN Number. • Enter correct PIN number upon
request.
• Choose Withdrawal from menu
list.
• Enter an amount to withdraw.
• Collect money from the money
output deck.
• Collect slip from the slip output
deck.
and a Happy Face !

Footer Text 10/30/2015 20


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• General’s Problem 
Withdraw Money from START
ATM INSERT ATM CARD
ENTER PIN NUMBER
o Examples of CHOOSE WITHDRAW
Pseudocode ENTER AN AMOUNT
COLLECT MONEY AND SLIP
• a notation resembling END
a simplified
programming
language, used in
program design

Footer Text 10/30/2015 21


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• General’s Problem  START

Withdraw Money from ATM


o Examples of Flow Chart INSERT
ATM CARD

• a graphical
representation of a ENTER

algorithm in relation to
PIN NUMBER

its sequence of CHOOSE


functions WITHDRAW

ENTER
AMOUNT

A
Footer Text 10/30/2015 22
8.2.3.1 Algorithm
Solve a problem using an Algorithm
A
• General’s Problem 
Withdraw Money from COLLECT

ATM MONEY AND SLIP

o Examples of Flow Chart END

• a graphical
representation of
algorithm in relation to
its sequence of
functions

Footer Text 10/30/2015 23


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• Math’s Problem 
Calculate total of START
two numbers. enter no1, no2
o Examples of Calculate total
Pseudocode total = no1 + no2
• a notation resembling a Display total
simplified programming END
language, used in
program design

Footer Text 10/30/2015 24


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• Math’s Problem  START

Calculate total of two


numbers. Input no1, no2

o Examples of Flow Chart


• a graphical Calculate total
representation of total = no1 + no2
algorithm in relation to its
sequence of functions

Display total

END

Footer Text 10/30/2015 25


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• Math’s Problem 
Finding an Area of a START
Rectangle
o Examples of
enter width, height
Pseudocode Calculate area of rectangle
• a notation resembling a area of rectangle = width x height
simplified programming
language, used in program
design
Display area of rectangle
END

Note:
** input/enter/key in/ read
** output / display / print
Footer Text 10/30/2015 26
8.2.3.1 Algorithm
Solve a problem using an Algorithm
• Math’s Problem  START

Finding an Area of
Rectangle Input width, height
o Examples of Flow Chart
• a graphical representation of
algorithm in relation to its
sequence of functions
area of rectangle = width x height

Display area of
rectangle

END

Footer Text 10/30/2015 27


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• Math’s Problem  Mean for 2 Marks
o Do Problem Analysis (prepare an IPO)

Input Processing Output

Footer Text 10/30/2015 28


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• Math’s Problem 
Mean for 2 Marks START
o Examples of ..
Pseudocode ..
• a notation resembling a
simplified programming ..
language, used in ..
program design
..

END

Footer Text 10/30/2015 29


8.2.3.1 Algorithm
Solve a problem using an Algorithm
• Math’s Problem  START

Mean for 2 Marks


o Examples of Flow Chart
• a graphical
representation of
algorithm in relation to
its sequence of
functions

END

Footer Text 10/30/2015 30


8.2.3.2 Algorithm
Control Structures

Sequence Selection Repetition


series of statements statement is used to statement is used to
that execute one determine which of repeat statements
after another two different while certain
statements to conditions are met
execute depending
on certain
conditions

Footer Text 10/30/2015 31


8.2.3.2 An Overview of

Control Structures
Sequence • all examples from previous slide
series of statements have already shown a
that execute one sequence control structures.
after another
An arrow shows the
execution of every
block (in a flow chart),
one after another, this
is a SEQUENCE
control structure to
solve a problem.

Footer Text 10/30/2015 32


8.2.3.2 An Overview of

Control Structures
• Example: How to make a tea drink
Start

Input Sugar, water, Tea bag

1. Put water into a cup


2. Add tea bag and sugar into a cup
3. Stir

Tea drink

End

Footer Text 10/30/2015 33


8.2.3.2 An Overview of

Control Structures
Selection
statement is used to
determine which of A diamond (in this
two different case is a SELECTION)
statements to determine whether the
resulting process is
execute depending TRUE or FALSE – and
on certain it will flow to one
conditions direction

Footer Text 10/30/2015 34


8.2.3.2 An Overview of

Control Structures
Selection
statement is used to
speed FALSE
determine which of
> 90
two different
statements to TRUE
execute depending
“ OVER LIMIT “
on certain
conditions

The program will display the message “Over limit” if


speed > 90

Footer Text 10/30/2015 35


8.2.3.2 An Overview of

Control Structures
Selection
statement is used to
determine which of UPS FALSE

two different > 50


statements to TRUE
execute depending
“ PASS“ “ FAIL “
on certain
conditions

The program will display the message “PASS” if


UPS > 50, otherwise, display message “FAIL”.

Footer Text 10/30/2015 36


8.2.3.2 An Overview of

Control Structures
Selection If the program receive an input of UPS = 60 , then
the data flow is presented by the dotted line ..
statement is used to
determine which of
two different
statements to
execute depending
UPS FALSE
on certain
> 50
conditions
TRUE

“ PASS“ “ FAIL “

Footer Text 10/30/2015 37


8.2.3.2 An Overview of

Control Structures
Selection If the program receive an input of UPS = 30 , then
the data flow is presented by the red line ..
statement is used to
determine which of
two different
statements to
execute depending
UPS FALSE
on certain
> 50
conditions
TRUE

“ PASS“ “ FAIL “

Footer Text 10/30/2015 38


8.2.3.2 An Overview of

Control Structures
UPS FALSE UPS FALSE
> 50 > 50

TRUE TRUE

“ PASS“ “ FAIL “ “ PASS“ “ FAIL “

Conclusion For Control Structure : SELECTION


decisions (selections): statement(s) is (are) executed if
certain condition gives TRUE or FALSE value.
Footer Text 10/30/2015 39
8.2.3.2 An Overview of

Control Structures
Repetition A diamond (in this case is
a REPETITION) if the
statement is used to value is TRUE, data will
repeat statements loop (in the loop body)
while certain and will out-of-loop when
the reach FALSE.
conditions are met

dashed line
show an area
of the Loop
Body

Footer Text 10/30/2015 40


8.2.3.2 An Overview of

Control Structures
Repetition If water boil is FALSE (meaning not yet boil to 100
degree Celsius, temp < 100 ), keep it boiling until the
statement is used to water is boiled (temp = 100 deg. Celsius)
repeat statements
while certain
conditions are met
FALSE
WATER
BOILING
BOIL ?

TRUE dashed line


show an area
of the Loop
Body

Footer Text 10/30/2015 41


8.2.3.2 An Overview of

Control Structures
Repetition If temp < = 100 is TRUE (meaning water still not
boil) keep it boiling until the temp reach 100, then it
statement is used to will stop.
repeat statements
while certain
conditions are met
TRUE
TEMP
BOILING
< = 100

FALSE
dashed line
show an area
of the Loop
now you can Body
make a coffee for
me !

Footer Text 10/30/2015 42


8.2.3.2 Apply Appropriate

Control Structures
START A

PUT WATER IN POUR WATER IN CUP


ELECTRIC KETTLE

ADD TEA BAG

TURN ON
KETTLE

SUGAR ?

FALSE
WATER
BOILING
BOIL ?
ADD
SUGAR
TRUE

A END
Footer Text 10/30/2015 43
8.2.3.2 Apply Appropriate

Control Structures
START A

PUT WATER IN
SEQUENCE POUR WATER SEQUENCE
IN CUP
ELECTRIC KETTLE

ADD
TEA BAG
TURN ON
KETTLE
SELECTION
REPETITION
NO
SUGAR ?

FALSE
WATER
YES
BOILING
BOIL ?
ADD
SUGAR
TRUE

A END
Footer Text 10/30/2015 44
8.2.3.2 Apply Appropriate

Control Structures
START A

POUR WATER
PUT WATER IN IN CUP
ELECTRIC KETTLE

ADD
TEA BAG
TURN ON
KETTLE

NO
SUGAR ?

FALSE
WATER
YES
BOILING
BOIL ?
ADD
SUGAR
TRUE

A END
Footer Text 10/30/2015 45
EXERCISE
• 1. (a) Write a pseudocode to display “ Kolej
Matrikulasi Perak”.

(b) Convert the pseudocode info flowchart.

• 2. (a) Write a pseudocode to calculate average of


two numbers.

(b) Convert the pseudocode info flowchart.

Footer Text 10/30/2015 46

You might also like