Download as pdf or txt
Download as pdf or txt
You are on page 1of 87

Prepared by:

Miss Norhamreeza Abdul Hamid


norhamreeza@gmail.com
+6013-7107067
Prepared by Norhamreeza Abdul Hamid
Basic problem solving techniques
Algorithm concept
Algorithm techniques
Plan an algorithm using pseudocode and
flowcharts

Prepared by Norhamreeza Abdul Hamid


Control
Algorithm Pseudocode Flowchart
Structure

Prepared by Norhamreeza Abdul Hamid


 Programming is a problem solving activity. When you write
a program, you are actually writing an instruction for the
computer to solve something for you.

 Problem solving is the process of transforming the


description of a problem into a solution by using our
knowledge of the problem domain and by relying on our
ability to select and use appropriate problem-solving
strategies, techniques and tools.

Prepared by Norhamreeza Abdul Hamid


Making a delicious Going to FKAAS from
chocolate cake. your hostel.

Installing a software Log into a system.


into a pc.

Prepared by Norhamreeza Abdul Hamid


How to make a delicious chocolate cake?

Prepare the ingredients.


Beat the butter and the eggs.
Mix with cake flour and sugar.
Add chocolate to the mixture.
Pour the mixture
Put the mixture in the oven.
Bake for 45 minutes.
Wipe chocolate cream around the baked
cake.
Prepared by Norhamreeza Abdul Hamid
How to install a software into a pc?

Put the software CD in the CD driver.


Open the CD directory in pc.
Click the installer file in the CD, install.exe.
Follow the setup instructions for installation
process.
Or use Control Panel > Add programs.

Prepared by Norhamreeza Abdul Hamid


How to log into an online system?

If you are a registered user,


•Enter your ID or username
•Enter your password
•Click on the submit button

If you are not registered, do the registration


process by filling in the registration form.

Prepared by Norhamreeza Abdul Hamid


How to go to FKAAS from
your hostel?

Prepared by Norhamreeza Abdul Hamid


Withdraw money from ATM machine
Register subjects through online system
Buy cold drinks from vending machine
Calculate the monthly salary payment
 Calculate car loan interest
 Determine odd and even numbers
 Count the total visitor of a Web
 Determine air velocity in physic lab
Prepared by Norhamreeza Abdul Hamid
Prepared by Norhamreeza Abdul Hamid
Writing computer program

Prepared by Norhamreeza Abdul Hamid


• Algorithm is a sequence of a finite
number of steps arranged in a specific
logical order to produce the solution for a
problem.

Prepared by Norhamreeza Abdul Hamid


Steps in
Computer program
for computer
to do its job

Famous muslim
scientist/
mathematician

Prepared by Norhamreeza Abdul Hamid


Procedure or formula
for solving problem

Sequence set of
steps taken to
solve a problem

Instructions for actions to be executed by computer

(input/process/output)

Prepared by Norhamreeza Abdul Hamid


Must have
input

Must produce
Efficiency
output

Algorithm
Requirements

Finiteness Unambiguous

Correctness Generality

Prepared by Norhamreeza Abdul Hamid


Step 1: Go to the taxi stand.
Step 2: Get in a taxi.
Step 3: Give the taxi driver the destination
address.
Step 4: Once arrived, pay the taxi fare.
Step 5: Get out from the taxi.

Prepared by Norhamreeza Abdul Hamid


Example 2: Money withdrawal algorithm
Step 1: Go to the ATM machine.
Step 2: Insert ATM card.
Step 3: Select language.
Step 4: Enter pin number.
Step 4: Select withdrawal option from menu.
Step 5: Enter amount of money to withdraw.
Step 6: Take the money.
Step 7: Take the bank slip.

Prepared by Norhamreeza Abdul Hamid


Do computer can really
understand algorithm?

Algorithm is used by programmer to plan


the instructions to be written in the
computer program and executed by
computer
Prepared by Norhamreeza Abdul Hamid
Prepared by Norhamreeza Abdul Hamid
Prepared by Norhamreeza Abdul Hamid
• Pseudocode is a semiformal, English-like language with
limited vocabulary that can be used to design & describe
algorithms.
• Purpose: to define the procedural logic of an algorithm in
a simple, easy-to-understand for its readers.
• Free of syntactical complications of programming
language.

Prepared by Norhamreeza Abdul Hamid


Simple codes which
resembles program code that
will be written during
implementation phase
(program coding)
A text-based design tool
that helps programmers to
develop algorithm
Using natural language
Compact and informal (communication
high level language language e.g:
description of a Malay/English) wording
computer programming
algorithm phrase
Prepared by Norhamreeza Abdul Hamid
i. Consists of a statement of instructions in sequence.
ii. Every steps consists of keyword which explain the
actions/operation/process to be executed by
computers
iii. Every steps should be written in different steps, if
continued, the next row must be indented.
iv. Can use if/else for conditional statement and while,
do/while and for repetition statement
v. Every steps should use clear statements and easy to
understand.
vi. Use start to represent the beginning of the operation
and end to show the end of the operation.
Prepared by Norhamreeza Abdul Hamid
Keyword?

START
print means
IF student's grade is greater than or equal to 60
PRINT the statement "passed" display on the
ELSE screen
PRINT the statement "failed" monitor
ENDIF
END
START show also
IF credit card number is valid means
execute transaction based on number and order display on the
ELSE screen monitor
SHOW a generic failure message
ENDIF
END
Prepared by Norhamreeza Abdul Hamid
set means
START assign the
SET total to zero
value,e.g:
SET grade counter to one
WHILE grade counter is less than or equal to ten total = 0
INPUT the next grade
ADD the grade into the total
ENDWHILE while is used
SET the class average to the total divided by ten whenever the
PRINT the class average.
process is to
END
be continued

Prepared by Norhamreeza Abdul Hamid


 Write the pseudo code based on the IPO
(input/process/output) which you have identified
during the analyzing phase
 Identify the keyword(s) based on the IPO

Did you remember the steps we’ve done in


Chapter 1?

Prepared by Norhamreeza Abdul Hamid


Identify the problem

Input Value of x and a

Process Replace the value of x and a in the


formula, y = 2x +a -6
Output The value of the equation, y

Prepared by Norhamreeza Abdul Hamid


Given the value of x is 10 and a is 12, Write a
find the result of the following equation: program!
y = 2x + a - 6
Keyword
Keyword
START
READ the value of x
READ the value of a
COMPUTE the value of y as y = 2x + a -6
DISPLAY/PRINT the result (or the value of y)
END
Prepared by Norhamreeza Abdul Hamid
Uncle Rahman wants to buy 5 tins of paint from Maria’s
shop. The price of each tin of the paint is RM 15.60.
Calculate the price which Uncle Rahman have to pay for
all the tin of paints he bought.
Write a
program!
START
READ the amount of paint
COMPUTE the total price as price per tin times by amount of
paint
DISPLAY/PRINT the result (or the total price) Keyword
END ?
Prepared by Norhamreeza Abdul Hamid
Mrs Sue needs to determine her students grade for
programming subject based on the mark scored during
final examination. The ‘A’ grade will be given if the mark
scored is between 85 to 100. If a student has scored 90
marks, what is the grade that Mrs Sue should give to the
student?

Write a program!

Prepared by Norhamreeza Abdul Hamid


Keyword

START
READ the student mark
IF mark is greater than 85 and mark is less than 100, then
set grade as A
ENDIF
DISPLAY/PRINT the grade Keyword
END

Prepared by Norhamreeza Abdul Hamid


Test your
understanding!

A box has height, width and length.


Write the pseudo code to calculate the volume
of a box.

Write the pseudo code to calculate salary of


an employee for a month.

Prepared by Norhamreeza Abdul Hamid


Given the following formula, write the
pseudocode
to calculate the porosity of rock:
porosity = (pure volume /total rock volume) x 100%

Prepared by Norhamreeza Abdul Hamid


START
INPUT/READ height, width, length
CALCULATE the volume = height * width * length
PRINT/DISPLAY volume
END

START
INPUT/READ salary per day
CALCULATE the salary= salary*30 days
PRINT/DISPLAY salary
END
Prepared by Norhamreeza Abdul Hamid
START
READ/INPUT pure volume, total rock
CALCULATE porosity = (pure volume / total rock
volume) x 100%
DISPLAY porosity
END

Prepared by Norhamreeza Abdul Hamid


Prepared by Norhamreeza Abdul Hamid
• Flowchart is a graph of geometrical shapes that are
connected by lines.
• 2 important elements in flowchart:
 Geometrical shapes – represent type of
statements in the algorithm.
 Flow line – show the order in which the
statements of an algorithm are executed.

Prepared by Norhamreeza Abdul Hamid


Schematic representation of an
algorithm or process

Helps to visualize the


content/steps better or to find
flaws in process
Illustrate the steps in the
process

Prepared by Norhamreeza Abdul Hamid


Start/Stop
The terminator symbol marks
the starting or ending point of
the system. It usually contains
the word "Start" or "End."
Action or Process
A box can represent a single
step ("add two cups of flour"),
or and entire sub-process
("make bread") within a larger
process.

Prepared by Norhamreeza Abdul Hamid


Decision
A decision or branching
point. Lines representing
different decisions emerge
from different points of the
diamond.
Input/Output
Represents material or
information entering or
leaving the system, such as
customer order (input) or a
product (output).

Prepared by Norhamreeza Abdul Hamid


Flow Line
Lines indicate the sequence
of steps and the direction of
flow.
Connector
Indicates that the flow
continues where a matching
symbol (containing the same
letter) has been placed.

Prepared by Norhamreeza Abdul Hamid


Off Page
Indicates that the process
continues off page.
Database
Indicates a list of information
with a standard structure that
allows for searching and
sorting.

Prepared by Norhamreeza Abdul Hamid


Prepared by Norhamreeza Abdul Hamid
What software use
to draw flowchart?

You can use


Ms word >
AutoShapes >
Flowchart or
other flowchart
drawing tools
e.g: SmartDraw/Visio
Drawing,etc.
Prepared by Norhamreeza Abdul Hamid
Given the value of x is 10 and a is 12,
find the result of the following equation:
y = 2x + a - 6

START
READ the value of x
READ the value of a
compute the value of y as y = 2x + a -6
DISPLAY/PRINT the result (or the value of y)
END
Prepared by Norhamreeza Abdul Hamid
Start

Read x

Read a

y = 2x + a - 6

Print y

End

Prepared by Norhamreeza Abdul Hamid


START
READ the amount of paint
compute the total price as price per tin times by amount of
paint
DISPLAY/PRINT the result (or the total price)
END Convert
to
START
READ the student mark flowchart
IF mark is greater than 85 and mark is less than 100, then
set grade as A
ENDIF
DISPLAY/PRINT the grade
END
Prepared by Norhamreeza Abdul Hamid
Start

Read amount of paint

total price = amount of paint * price per tin

Display total price

End
Prepared by Norhamreeza Abdul Hamid
A box has height, width and length.
Write the pseudo code to calculate the volume
of a box.
Convert
to
flowchart
Write the pseudo code to calculate salary of
an employee for a month.

Prepared by Norhamreeza Abdul Hamid


Convert to
flowchart

Given the following formula, write the pseudo code


to calculate the porosity of rock:
porosity = (pure volume /total rock volume) x 100%

Prepared by Norhamreeza Abdul Hamid


Start

Read height, width,


length

volume = height * width * length

Display volume

End
Prepared by Norhamreeza Abdul Hamid
Start

Read salary per day

salary = salary per day * 30 days

Display salary

End
Prepared by Norhamreeza Abdul Hamid
Start

Read pure volume,


total rock

porosity = (pure volume / total rock


volume) x 100%

Display porosity

End
Prepared by Norhamreeza Abdul Hamid
Prepared by Norhamreeza Abdul Hamid
• Sequential
• Selection
• Repetition

Prepared by Norhamreeza Abdul Hamid


Sequence: A series of steps or statements that
are executed in the order they are written in an
algorithm.
Selection: Defines two courses of action
depending on the outcome of a condition. A
condition is an expression that is, when
computed, evaluated to either true or false.
Repetition: Specifies a block of one or more
statements that are repeatedly executed until a
condition is satisfied.

Prepared by Norhamreeza Abdul Hamid


You may have more
than one control
structure in one program
in order to solve a
problem.

Prepared by Norhamreeza Abdul Hamid


 A series of steps or statements that are executed
in the order they are written in an algorithm.
 The beginning and end of a block of statements
can be optionally marked with the keywords
begin and end.
start
START
statement 1
statement 1.
statement 2. statement 2



statement n. statement n

END end
Prepared by Norhamreeza Abdul Hamid
Problem: calculate a person’s age

START start

READ birth year


age = current year – birth year read birth year

DISPLAY age
END Age = current year –
birth year

Display age

end

Prepared by Norhamreeza Abdul Hamid


 Defines two courses of action depending on the
outcome of a condition. A condition is an
expression that is, when computed, evaluated to
either true (1) or false (0).
 The keyword used are if and else.

Prepared by Norhamreeza Abdul Hamid


 Format:
No Yes
Condition?
IF (condition)
then-part
ELSE
else- then-
else-part statement(s) statement(s)
ENDIF

Prepared by Norhamreeza Abdul Hamid


START
READ age
IF age is greater than 55 start
PRINT “Pencen”
ELSE Read age
PRINT “Kerja lagi”
ENDIF
END yes no
age > 55

START
READ age print “Pencen” print “Kerja lagi”

IF age > 55
PRINT “Pencen”
ELSE
End
PRINT “Kerja lagi”
ENDIF
END

Prepared by Norhamreeza Abdul Hamid


 Sometimes in certain situation, we may omit the else-part.
IF number is odd number Example 1
PRINT “This is an odd number”
ENDIF

 Nested selection structure: basic selection structure that


contains other if/else structure in its then-part or else-part.
IF number is equal to 1
print “One”
ELSEIF number is equal to 2
print “Two” Example 2
ELSEIF number is equal to 3
print “Three”
ELSE
print “Other”
ENDIF

Prepared by Norhamreeza Abdul Hamid


Draw the flowchart diagram for
Example 1 and Example 2

Prepared by Norhamreeza Abdul Hamid


Specifies a block of one or more
statements that are repeatedly executed
until a condition is satisfied.
The keyword used is while.

Prepared by Norhamreeza Abdul Hamid


Format:

WHILE (condition)
loop-body Condition?
yes Loop
Statement(s)
ENDWHILE
no

Prepared by Norhamreeza Abdul Hamid


Counter
Can be used to control execution of the loop
(loop control variable)
It will increment or decrement each time a
loop repeats
Must be initialized before entering loop

Prepared by Norhamreeza Abdul Hamid


Start

initialization
cnt=0

FALSE
condition FALSE
cnt<5 End
TRUE
TRUE
body of loop
Print “whatever”

increment
cnt=cnt+1

Prepared by Norhamreeza Abdul Hamid


Problem: Write a program that reads and displays
the age of 10 people (one after another).

For this problem, we need a way to count how many


people whose age have been processed (read and
displayed). Therefore, we introduce a concept of counter,
a variable used to count the number of people whose
age have been processed by the program.

Prepared by Norhamreeza Abdul Hamid


START Counter initialisation
number of users giving his age = 1
WHILE number of users giving his age <= 10
READ the age from the user.
PRINT the user age. Loop condition
number of user giving his age + 1
ENDWHILE
Updating counter
END

START
users = 1
WHILE users <= 10
READ age
PRINT age.
users = users + 1
ENDWHILE
End

Prepared by Norhamreeza Abdul Hamid


Start

users = 1

NO
End users <= 10?

YES

read age

print age

users =users + 1

Prepared by Norhamreeza Abdul Hamid


START You can start the
counter with ZERO
number of users giving his age = 0
WHILE number of users giving his age < 10
READ the age from the user.
PRINT the user age.
number of user giving his age + 1 The loop condition
must less than the
ENDWHILE value it requires to
END stop

START
users = 0
WHILE users < 10
READ age
PRINT age. Be
users = users + 1 consistent
ENDWHILE
END

Prepared by Norhamreeza Abdul Hamid


Now let us put together everything that
you have learnt so far.

Problem:
Write a program that will calculate and print
the age of 10 persons, given their birth year. If
the age of the person is above 55, then the
program will print “Pencen”, otherwise, the
program will print “Kerja lagi”.

Prepared by Norhamreeza Abdul Hamid


START
users = 1
WHILE users <= 10 Example 3
Read birth year
age = current year – birth year
print age
IF age > 55
print “Pencen” Note that in this
ELSE example, we are
using all the three
print “Kerja lagi” control structures:
ENDIF sequence, selection
users = users + 1 and repetition
ENDWHILE
END

Prepared by Norhamreeza Abdul Hamid


Draw the flowchart diagram for
Example 3

Prepared by Norhamreeza Abdul Hamid


Prepared by Norhamreeza Abdul Hamid
Write pseudocode and flowchart for :
1) read name and display name
2) compute and print the sum of all integers
between 1 and 10.
3) read an integer and determine whether it is
odd or even.
4) determine whether profit, return capital or
loss.
5) determine whether the marks is less than
40%. If it is less than 40, then print “fail”,
otherwise print “pass”.

Prepared by Norhamreeza Abdul Hamid


6) determine whether the speed limit exceeds
110 km per hour. If the speed exceeds 110,
then fine = 300, otherwise fine = 0. Display
fine.
7) determine whether the speed limit exceeds
110 km per hour. If the speed exceeds 110,
then fine = 300, otherwise fine = 0. Display
fine.
8) determine whether the age is above 12
years old. If the age is above 12, then ticket
= 20, otherwise ticket = 10. Display ticket.

Prepared by Norhamreeza Abdul Hamid


9) read two numbers and identify the largest
number between the two numbers, number1
and number2. If number2 largest than
number1, then there would be a message
“The second number is largest than the first
number”. If the numbers are equal, there
would be a message “These numbers are
equal”. Otherwise, “The first number is largest
than second number”

Prepared by Norhamreeza Abdul Hamid


9) read two numbers and identify the largest
number between the two numbers, number1
and number2. If number2 largest than
number1, then there would be a message
“The second number is largest than the first
number”. If the numbers are equal, there
would be a message “These numbers are
equal”. Otherwise, “The first number is largest
than second number”

Prepared by Norhamreeza Abdul Hamid


Prepared by Norhamreeza Abdul Hamid
Your summer surveying job requires you to
study some maps that give distances
kilometers and some that use miles. You
and your coworkers prefer to deal
measurements. Design the pseudocode
and flowchart that performs the necessary
conversion.

Prepared by Norhamreeza Abdul Hamid


The unit for electricity usage is kWh. For domestic
usage, the monthly rate is 21.8 cents/unit for the
first 200 unit, 25.8 cents/unit for the next 800 units
and 27.8 cents/unit for each additional units. Given
the amount of electricity units (in kWh) used by a
customer, calculate the amount of money needs
to be paid by the customer to TNB. A bill statement
needs to be printed out.
Write a pseudocode and a flow chart to solve the
above problem.

Prepared by Norhamreeza Abdul Hamid


A hotel has a pricing policy as follows:
- 2 people : RM85
- 3 people : RM90
- 4 people : RM95
- Additional people : RM6 per person
If the customer is staying on company business,
there is a 20% discount. If the customer is over 60
years of age, there is a 15% discount. A customer
does not receive both discount. Given the above
data, print the cost of the room and design the
solution using pseudocode and flowchart.

Prepared by Norhamreeza Abdul Hamid


The National Earthquake Information center has
asked you to write a program implementing the
following decision table to characterize an
earthquake based on its Richter scale number.
Richter scale number (n) Characterization
n < 5.0 Little or no damage
5.0 <=n < 5.5 Some damage
5.5 <= n < 6.5 Serious damage
6.5 <= n < 7.5 Disaster
Higher Catastrophe

Given the problem above, design the solution


using pseudocode and flowchart.
86 Prepared by Norhamreeza Abdul Hamid
Prepared by Norhamreeza Abdul Hamid

You might also like