Sie sind auf Seite 1von 63

CHAPTER 2

PROBLEM SOLVING METHODS


FP 101
PROGRAMMING PRINCIPLES
1
Course Learning Outcome
(CLO):
2
Upon completion of this course, students should be able to:

explain the basic computer and programming fundamentals with
appropriate example of languages.

practice different types of problem solving method to solve
problem efficiently.

solve problems by applying related theories of the basic
programming technique to a given particular scenario using
programming life cycle.
Specific outcome:
2. 1 Understand problem solving concept.
2.2 Demonstrate the understanding of
Programming Life Cycle.
2.3 Apply the different types of algorithm to
solve problem.
2.4 Use problem solving tools to solve
problem

3
2.1 Understand Problem
Solving Concept
4
Problem Solving Concept
Identifying the problem--Which problem should I address? If
there are several, how do I choose the most important one?
Describing the problem--How do I accurately and completely
describe the problem?
Analyzing the problem--What are the different causes of the
problem, and which causes are most important to solve right
away?
Planning the solutions--What are the different alternative
solutions for solving the problem?
Implementing the solutions--How do I make sure the solutions
are implemented correctly and effectively?
Monitoring/evaluating the solutions--How did the solutions
work? What needs to be changed?
5
Developing the Input Process Output
(IPO) Chart
Extends and organizes the information in the Problem
Analysis Chart.
It shows in more detail what data items are input, what
are the processing or modules on that data, and what
will be the result or output.
Input Processing Output
All input
data from
PAC
All processing steps
from IPO / IC
All output
requirements
from PAC
Define Input, Process
And Output
Developing the Input Process Output
(IPO) Chart
Problem
Write a Input Process Output (IPO) to find an area of a
circle where area = pi * radius * radius
Input Processing Output
- radius - Area = 3.14 x radius x radius
-Display area
- Area of a circle
Identify Input, Process
And Output
A program is required to read three (3) numbers,
add them and print their total. It is usually helpful
to write down the three (3) components in a
defining diagram as shown below:
Example
Steps Of Program Execution
Input Refers to the process of entering data, program and
instructions into the computer system using input devices.
Process Computer processes raw data into usable information to
be used by user. Data processing is done by the CPU (Central
Processing Unit).
Output Output is raw data that has been processed by the
computer (result). Output will be converted to an understandable
form before being displayed or printed.

9
Example: Flow Of ATM
Program
Assume that our transaction is money
withdrawal. The instructions are:
a. Get the card number from the user
b. Get pin number from the user
c. Process the input data
d. Get the transaction chosen by the user
e. Get the account type from the user
f. Process the transaction as wanted by the user
g. Withdraw amount of money required by the user
h. Print receipt for the user
10
(Input)
(Input)
(Input)
(Input)
(Process)
(Process)
(Output)
(Output)
Input: Example: Card number, ATM pin number, type of
transaction, type of account, amount of money to
withdraw.
Process: Example: Process to identify card number, valid pin
number, type of transaction, type of account and deducts
the withdrawal from the users account.
Output: Example: Receipt will show balance in users account and
money withdrawn.

INPUT
PROCESS OUTPUT
11
Executing A Program
To execute a program, CPU will examine each program
instruction in memory and send out the required command
signals to carry out the instruction.
During execution, data can be entered into memory and
manipulated in some specific way (delete and modify) as
required.
Program instructions are used to copy a program's data
(program input) into memory.
After the input data were processed, instructions for
displaying or printing will be executed.
Result displayed by a program is called program output.
Example: Cash withdrawal from ATM machine.

12
First step:
Data entered by user are stored in memory (input).
Second step:
CPU will instruct program to process the card number and ATM
pin number with the data in memory concurrently. Program will
execute the transaction chosen by the user and store the result in
memory.
Third step:
The outputs are money withdrawal and receipt (output).

Machine language
program for processing
card number and PIN
number.
Data entered during
execution.
Computed results.
Central Processing Unit.
Output results:
Receipt and money
Input data:
Card Number,
PIN Number,
transaction.
Program Output
Step 1
Step 2
Step 3
13
Discuss
14
1. Show step of mailing a copy of your SPM
transcript using photocopy machine.
2. Show step of making call from your
telephone.
3. Show step of saving your document in your
computer.
4. Show steps of buying a tin of soft drink for a
vending machine.
5. Show steps of sending an email to your
friend.

ACTIVITY 1: GRAFFITI
15
1. Everyone get a piece of graphic paper
2. Recall what you have learn in chapter 1 and chapter
2 (2.1)
3. Write down 4 things that you have learned in class
(only 3 parts)- 4 minutes
4. Move to your friends in class and share the answer
(1 friend=1 answer)
5. Move again to other friends and share the answer
until you fullfill 4 PARTS of PAPER
6. I will call 4 people to share the answer
2.2 Demonstrate the
understanding of
Programming Life Cycle
16
Programming Life Cycle
Life Cycle: Refers to the changes made from
an old to a new program (the cycle starts
again).
Programming Life Cycle: A framework or
discipline, which uses certain techniques
needed in computer programming
development.
17
Steps involved in Programming
Life Cycle:
1. Specify the
problem
2. Analyze the
problem
3. Program
design(the
algorithm)
4. Program
coding(Implem
ent the
algorithm)
5. Test and
verify
6. Maintain and
update
7.
Documentation
18
Step 1: Specify The Problem
Describe exactly what you are doing as clear as
possible
the data to be used, and what can be assumed.
Also, the desired output and its layout.
Specification of needs
what the problem is
what is needed to solve it
what the solution should provide
if there are constraints and special conditions.


19
Example
20
Situation:
Joe Jambul was borrowing a book form PBU library.
He was late returning a book late. Calculate the
total fine charged to Joe Jambul by library for late-
return books. The charge is RM0.20 for 1 day.




Calculate the total fine charged to Joe Jambul by
library for late-return books.
Data given (RM 0.20/day)



Specify the problem:

Step 2: Problem Analysis
To describe in detail a solution to a problem and information
needed in solving the problem. Study and understand the
problem.
Identify:
The needed input.
The needed process.
The required output.
Special constraints (if any)
Formulas or equations to be used



21
Step 3: Program Design
Definition:
It is a framework or flow that shows the steps in
problem solving.






22
Methods to design a program:
1. Algorithm
Algorithm is a sequence of instructions to solve a
problem, written in human language, and it can solve
any problems when it is used with the correct
procedure. (Example)
2. Flowchart
A graphical representation of data, information and
workflow using certain symbols that are connected to
flow lines to describe the instructions done in problem
solving.
It shows the flow of the process from the start to the
end of the problem solving. (Example)

Step 3: Program Design cont.
23
25
ACTIVITY 2:DISCUSSION
(20 minutes)
26
1. Create 8 group (7 people/group)-
2 minutes
2. Get a topic, marker pen, paper
3. Discuss in group
4. Write the algorithm in paper given
5. Choose BEST GROUP as
WINNER
(faster, correct , creative and
tidiness)

PROBLEM
27


You want to calculate the total
and average of four number that
entered by yourself. Then,
display the total and average.


Methods to design a program:
Basic flowchart symbol
Step 3: Program Design
29
Symbol Explanation
Indicate the direction of data flow.
Used to connect a block to another
block.
Indicates operations / process
involved.
Receive / read value
Display value
Flow Lines
Process
Input / Output
Methods to design a program:
Basic flowchart symbol
Step 3: Program Design
30
Symbol Explanation
Execute decision based on
condition.
Test is performed and the
program flow continues, based on
the result
Indicates the beginning and end of
a flowchart.
Show the continuing flowchart in
the same page.
Decision
Start / End Flow
Lines
On-page
connector Flow
Lines
Methods to design a program:
Basic flowchart symbol
Step 3: Program Design
31
START
Input
Condition
False statement
Output
END
False
True Statement
True
Methods to design a program:
Flowchart symbol usage
Step 3: Program Design
32
Flowchart to calculate the total of fine for late returning of library books.
RM0.20 perday

START
Input
total_of_day
Fine = total_of_day * 0.20
Output Fine
END
33
Example Flowchart
Step 3: Program Design
ACTIVITY 2:DISCUSSION
(20 minutes)
34
1. Create 8 group (7 people/group)-
2 minutes
2. Get a topic, marker pen, paper
3. Discuss in group
4. Draw the flowchart in paper given
5. Choose BEST GROUP as
WINNER
(faster, correct , creative and
tidiness)

Methods to design a program:
3. Pseudocode
Steps in problem solving that is written half in
programming code and half in human language.
Advantages:
Easily understood.
Easily maintained.
The codes are changeable.

Disadvantages:
Cannot be executed in the computer.

Step 3: Program Design
35
Example of Pseudocode
36
START
Input quiz1, quiz2, quiz3
TotalMark= quiz1+quiz2+quiz3
Print TotalMark
END


ACTIVITY 2:DISCUSSION
(20 minutes)
37
1. Create 8 group (7 people/group)-
2 minutes
2. Get a topic, marker pen, paper
3. Discuss in group
4. Draw the pseudocode in paper
given
5. Choose BEST GROUP as
WINNER
(faster, corret , creative and tidiness)

Methods to design a program:
4. Structure Charts
An additional method in preparing programs that
has many sub modules.
It consists rectangular boxes, which represents all
the sub modules in a program and is connected
by arrows.
It illustrates the top-down design of a program
and is also known as hierarchical chart because
its components are in hierarchical form.
The advantage is that it is easy to be drawn and
to be changed.
Step 3: Program Design
38
Structure Chart:
The problem is normally big and complex.
Thus, requires big program.
Thus, the processing can be divided into
subtasks called modules.
Each module accomplishes one function.
These modules are connected to each
other to show the interaction of processing
between the modules.
Methods to design a program:
Step 3: Program Design
Methods to design a program:
Structure chart
Step 3: Program Design
40
Problem
Write a Structure Charts to find an area of a circle
where area = pi * radius * radius

Area
radius
area = 3.14 x
radius x radius
Display area
Example Structure Chart
Step 3: Program Design
Problem: To calculate the amount of water bill

Represent the modules in the program
Water bill
Get /
read
data
Compute
the
charge
Display
instructions
for user
Determine
the late
charge
Display
the bill
Print
the bill
42
Example Structure Chart
Step 3: Program Design
Steps 4: Program Coding
Definition: Writing problem solving into certain
programming language such as C, COBOL , C++
and others.
Problem solving: Instructions before it is coded
into programming language.
Purpose: To produce a program to develop a
system
The process of implementing an algorithm by
writing a computer program using a programming
language (for example, using C or C++ language)
The output of the program must be the solution of
the intended problem.

43
Step 5: Testing And Verify
Testing :
Using a set of data to discover errors and to
ensure accuracy of the program.
Process of testing: is the process of executing a
program to demonstrate its correctness


Program verification is the process of ensuring
that a program meets user-requirement

44
Two types of error:
1. Syntax Error (grammatical error)
Occurs when the rules of programming
language are not applied.
Correction is done during the program
coding.
The bug can be traced during the
compilation.
Also known as compile-time error
Must be corrected before executing and
testing the program.
Step 5: Testing And
Debugging
45
2. Logic error
Cannot be traced by compiler.
Corrected during the problem solving
process.
Also known as run time error.
Example : output for average is 4, but when
it runs, the output is 2.
Two types of error:
Step 5: Testing And
Debugging
46
Step 6: Maintain And
Update
Definition:
Activity that verifies whether the operational system is
performing as planned or an activity to modify the
system to meet the current requirement.
The process of changing a system after it has been
applied to maintain its ability. The changes may
involve simple changes such as error correcting.
Process of changing a system after it has been
delivered and is in use.
Adaptive Maintenance - modifications to properly
interface with changing environments -> new
hardware, OS, devices
Perfective Maintenance - implementing new system
requirements after system is successful

47
How to do maintenance?
Testing Test the ability of the system.
Measurement Access data time. Example, time to
save, print and others.
Replacement Replace the old system to new
system.
Adjustment Adding needs to new system.
Repair For example: An old system cannot update
the new data
Updating Update the database.
Step 6: Maintenance
48
Step 7: Documentation
Definition: A written or graphical report of
the steps taken during the development of
a program.

Purpose: It will be useful in the future, in
case of modification or maintenance.
49
Content of Documentation:

Description of the program.
Specification of program requirement
Program design such as pseudocode and flowchart
List of program and comments (to explain about the
program).
Test results.
Users manual book.
Program capabilities and limitation.
Step 7: Documentation
50
Two Pair Sharing
51
In 5 minutes, write down
anything that you have learned
in chapter 2.
Choose your pair and share it.
(5 minutes)
Pick 3 students from magic box
Example 1

Write a program that will get 3 numbers as
input from the users. Find the average and
display the three numbers and its average.

Step 1: Define Problem
52
Step 2: The Problem Analysis
Input:
numbers1, number2 and number3.
Process:
totalNumbers=number1+number2+number3
average=totalNumbers/3
Output:
The three numbers and its average
Example 1

INPUT

PROCESS

OUTPUT
53
To calculate the average of three numbers.

1. Set Total=0, Average=0;
2. Input 3 numbers
3. Total up the 3 numbers
Total= total of 3 number
4. Calculate average
Average=Total/3
5. Display 3 numbers and the average


Step 3: The Program Design
(using Algorithm)
Example 1
54
55
Step 3: The Program Design
(using Pseudocode)
Example 1
56
Step 4: The Program Coding
Example 1
#include<iostream.h>
void main()
{
float a,b,c,sum,av;
cout<<"Enter three numbers:";
cin>>a>>b>>c;

sum=a+b+c;
av=sum/3;
cout<<"\nSUM="<<sum;
cout<<"\nAverage="<<av;

getch(); //to stop the screen
}
2.3 Apply The Different
Types Of Algorithm To Solve
Problem
57
Concept Of Algorithm
An algorithm is a list of steps to be executed
with the right order in which these steps
should be executed.
An algorithm can be represented using
pseudocode or flowchart.
Any algorithm can be described using only 3
control program structures: sequence,
selection and repetition.
Concept Of Algorithm
Algorithm:
Must may have input(s) and must have output(s)
Should not be ambiguous (there should not be different
interpretations to it)
Must be general (can be used for different inputs)
Must be correct and it must solve the problem for which
it is designed
Must execute and terminate in a finite amount of time
Must be efficient enough so that it can solve the
intended problem using the resource currently
available on the computer

Distinguish Between Flowchart
And Pseudocode
TYPES FLOWCHART PSEUDOCODE
Layout Graphical structure Structure for the
code of the program
Benefits For smaller
concepts and
problems
More efficient for
larger programming
languages
Structure Symbols and
shapes
Linear text-based
structure
Depth Detail can cause
confusion
More flexibility with
detail
Example 1
61
A flowchart (and equivalent Pseudo code) to
compute the interest on a loan

Flowchart Pseudocode
Example 2
62
A program that reads two numbers and displays
the numbers read in decreasing order

Flowchart Pseudocode
Read A, B
If A is less than B
BIG = B
SMALL = A
else
BIG = A
SMALL = B
Write (Display) BIG, SMALL

TIME FOR ACTIVITY!!
(30 minutes)
63
Form a group
Leader in group find the question in class
Go to your group
Discuss with your group member
Write your answer in Majung Paper
(Algorithm, flowchart, pseudocode)
Choose BEST GROUP as WINNER
(faster, correct , creative and tidiness)


EXERCISE
64
1. Write IPO, pseudo code and flow chart for
the program which can calculate total
sale by multiply quantity and price.
2. Write an IPO, pseudo code and flow chart
for this program: any students whom got
CGPA more than 2.0 will pass their study,
and less than 2.0 will fail.


65

Das könnte Ihnen auch gefallen