Transcript
Page 1: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Page 2: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Computer

programmer

WHAT Program ??

Page 3: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Steps used to solve problem in computer programming

Also known as Software/ System

Development Life Cycle (SDLC) or software

process

A development of a software/computer

program product.

Page 4: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Specify problem

Analyze problem

Design algorithm

Implement algorithm

Test and verify program

Maintain and update program

Steps

Page 5: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

STATE What problem to be solved?

What is the problem?

Is it possible to solve

the problem with

programming?

Page 6: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

State the problem clearly Gain a clear

understanding of

what are

required for its

solution.

Page 7: Chapter 2 - Problem Solving Techniques [Part2]I

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

What is the problem?

Is it possible to

solve the problem

with

programming?

What to solve?

Page 8: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Problem What is the value of y?

Data Given the value of x and a

Solution Solve the equation using the given values

State the problem clearly

Page 9: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

IDENTIFY

Input Output

Process

Page 10: Chapter 2 - Problem Solving Techniques [Part2]I

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

Input

Output

Process

Page 11: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

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

Identify the problem

Page 12: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Develop a list of steps

called algorithm

Verify that the algorithm

solves the problem

Solve Problem

Page 13: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Convert algorithm into

programming

Writing program

Also known as

CODING

Page 14: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Coding

General steps involved

Documentation

Compile

Execute/Run

Write program code with

specific programming language

in specific programming

tools/software Write explaination/description

for

each line of codes/comment for

future references

Using tool/software function to

check errors in program code

Using tool/software function

to display output of program

Page 15: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

To verify the program can

works as desired

Testing program

Executing program to see if it

produce correct results.If not

do debugging.

Page 16: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Remove undetected errors

and keep up-to-date

Modifying program

USE and MODIFY the program

to meet changing requirements

or correct errors that show up

in using it

Page 17: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Specify problem

Analyze problem

Design algorithm

Implement algorithm

Test and verify program

Maintain and update program

Steps

Page 18: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Algorithm Pseudocode Flowchart Control

Structure

Page 19: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 20: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Going to FSKTM from

FKAAS .

Making a delicious

chocolate cake.

Installing a software

into a pc.

Log into a system.

Page 21: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 23: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 24: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

How to go to FSKTM from FKAAS?

Page 25: Chapter 2 - Problem Solving Techniques [Part2]I

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

Page 26: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Page 27: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Writing computer program

Page 28: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 29: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Steps in

Computer program

for computer

to do its job

Famous muslim scientist/

mathematician

Page 30: Chapter 2 - Problem Solving Techniques [Part2]I

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)

Page 31: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Algorithm Requirements

Must have input

Must produce output

Unambiguous

Generality Correctness

Finiteness

Efficiency

Page 32: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 33: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Step 1: Go to the ATM machine.

Step 2: Insert ATM card.

Step 3: Select language.

Step 4: Enter pin number.

Step 5: Select withdrawal option from menu.

Step 6: Enter amount of money to withdraw.

Step 7: Take the money.

Step 8: Take the bank slip.

Example 2: Money withdrawal algorithm

Page 34: Chapter 2 - Problem Solving Techniques [Part2]I

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

Page 35: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Page 36: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Page 37: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 38: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

A text-based design tool that helps programmers to develop algorithm

Using natural language (communication

language e.g: Malay/English) wording

phrase

Compact and informal

high level language description of a

computer programming

algorithm

Simple codes which

resembles program code that

will be written during

implementation phase

(program coding)

Page 39: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 40: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

START

IF student's grade is greater than or equal to 40

PRINT the statement "passed"

ELSE

PRINT the statement "failed"

ENDIF

END

Keyword?

print means display on the

screen monitor

START

IF credit card number is valid

execute transaction based on number and

order

ELSE

SHOW a generic failure message

ENDIF

END

show also means

display on the screen monitor

Page 41: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

START

SET total to zero SET grade counter to one

WHILE grade counter is less than or equal to

ten

INPUT the next grade

ADD the grade into the total ENDWHILE

SET the class average to the total divided

by ten PRINT the class average.

END

set means assign the value,e.g: total = 0

while is used whenever

the process is to be

continued

Page 42: Chapter 2 - Problem Solving Techniques [Part2]I

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

earlier?

Page 43: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

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

Identify the problem

Page 44: Chapter 2 - Problem Solving Techniques [Part2]I

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

SET the value of x to 10 SET the value of a to 12 COMPUTE the value of y as y = 2x + a - 6 DISPLAY/PRINT the result (or the value of y)

END

Write a

program!

Keyword Keyword Keyword

Page 45: Chapter 2 - Problem Solving Techniques [Part2]I

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.

START SET the amount of paint to 5 SET the price per tin to 15.60 COMPUTE the total price as price per tin times by amount of paint DISPLAY/PRINT the result (or the total price)

END

Write a

program!

Keyword

?

Page 46: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

START

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

END

Keyword Keyword

Keyword

Page 47: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

A box has height, width and length. Write the pseudo code to calculate the volume of a box.

Test your

understanding!

Write the pseudo code to calculate salary of an employee for a month.

Page 48: Chapter 2 - Problem Solving Techniques [Part2]I

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 per day*30 days PRINT/DISPLAY salary

END

Page 49: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

START READ/INPUT pure volume, total rock CALCULATE porosity = (pure volume / total rock volume) x 100% DISPLAY porosity

END

Page 50: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Page 51: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 52: Chapter 2 - Problem Solving Techniques [Part2]I

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

Page 53: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Start/End

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.

Page 54: Chapter 2 - Problem Solving Techniques [Part2]I

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).

Page 55: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 56: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 57: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 58: Chapter 2 - Problem Solving Techniques [Part2]I

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

SET the value of x to 10 SET the value of a to 12 compute the value of y as y = 2x + a -6 DISPLAY/PRINT the result (or the value of y)

END

Page 59: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Start

End

y = 2x + a - 6

Print y

Set x = 10

Set a = 12

Page 60: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

START SET the amount of paint to 5 COMPUTE the total price as price per tin times by amount of paint DISPLAY/PRINT the result (or the total price)

END Convert

to

flowchart

Page 61: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Start

total price = amount of paint * price per tin

Display total price

End

Set amount of paint = 5

Page 62: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

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.

Convert

to

flowchart

Convert

to

flowchart

Page 63: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

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

Convert to

flowchart

Page 64: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Start

Read height, width,

length

volume = height * width * length

Display volume

End

Page 65: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Start

Read salary per day

salary = salary per day * 30 days

Display salary

End

Page 66: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Start

Read pure volume,

total rock volume

porosity = (pure volume / total rock

volume) x 100%

Display porosity

End

Page 67: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Page 68: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

• Sequential • Selection • Repetition

Page 69: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 70: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

You may have more

than one control

structure in one program

in order to solve a

problem.

Page 71: Chapter 2 - Problem Solving Techniques [Part2]I

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

START and END.

START

statement 1.

statement 2.

statement n.

END

statement 1

statement 2

statement n

start

end

Page 72: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

START

READ birth year, current year

CALCULATE age = current year –

birth year

DISPLAY age

END

Problem: calculate a person’s age

READ birth year – birth year

Age = current year – birth year

Display age

start

end

Page 73: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 74: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Format:

IF (condition)

then-part

ELSE

else-part

ENDIF

Condition?

else-

statement(s)

then-

statement(s)

Yes No

Page 75: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

start

Read age

End

age > 55 no yes

print “Pencen” print “Kerja lagi” START

READ age

IF age > 55

PRINT “Pencen”

ELSE

PRINT “Kerja lagi”

ENDIF

END

START

READ age

IF age is greater than 55

PRINT “Pencen”

ELSE

PRINT “Kerja lagi”

ENDIF

END

Page 76: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 77: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Format:

WHILE (condition)

loop-body

ENDWHILE Condition?

Loop

Statement(s)

yes

no

Page 78: Chapter 2 - Problem Solving Techniques [Part2]I

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

Page 79: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

initialization

condition

body of loop

increment

FALSE

TRUE

cnt=0

cnt<5

cnt=cnt+1

FALSE

TRUE

Start

Print “whatever”

End

Page 80: Chapter 2 - Problem Solving Techniques [Part2]I

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.

Page 81: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

START

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.

number of user giving his age + 1

ENDWHILE

END

START

users = 1

WHILE users <= 10

READ age

PRINT age.

users = users + 1

ENDWHILE

End

Counter initialisation

Loop condition

Updating counter

Page 82: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

Start

End users <= 10? NO

YES

users = 1

print age

read age

users =users + 1

Page 83: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid

START

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

ENDWHILE

END

START

users = 0

WHILE users < 10

READ age

PRINT age.

users = users + 1

ENDWHILE

END

The loop condition

must less than the

value it requires to

stop

You can start the

counter with ZERO

Be consistent

Page 84: Chapter 2 - Problem Solving Techniques [Part2]I

Prepared by Norhamreeza Abdul Hamid