Upload
muhd-rzwan
View
224
Download
0
Embed Size (px)
Citation preview
7/29/2019 Chapter2 ProblemSolving New
1/31
Principles of Programming - NI July 2005 1
Chapter 2: Problem Solving
In this chapter you will learn about:Introduction to Problem Solving
Software development method (SDM)
Specification of needsProblem analysis
Design and algorithmic representation
Implementation
Testing and verificationDocumentation
7/29/2019 Chapter2 ProblemSolving New
2/31
7/29/2019 Chapter2 ProblemSolving New
3/31
Principles of Programming - NI July 2005 3
Software Development Method (SDM)
1. Specification of needs2. Problem analysis
3. Design and algorithmic representation
4. Implementation5. Testing and verification
6. Documentation
7/29/2019 Chapter2 ProblemSolving New
4/31
Principles of Programming - NI July 2005 4
Specification of Needs
To understand exactly:what the problem is
what is needed to solve it
what the solution should provideif there are constraints and special
conditions.
7/29/2019 Chapter2 ProblemSolving New
5/31
Principles of Programming - NI July 2005 5
Problem Analysis
In the analysis phase, we should identify thefollowing:
Inputs to the problem, their form and the input
media to be used
Outputs expected from the problem, their form
and the output media to be used
Special constraints or conditions (if any)
Formulas or equations to be used
7/29/2019 Chapter2 ProblemSolving New
6/31
Design and algorithmic representation
Can be represented using pseudocodes orflowcharts
Principles of Programming - NI July 2005 6
7/29/2019 Chapter2 ProblemSolving New
7/31
Principles of Programming - NI July 2005 7
Control Structure
In order to tackle a problem, we needa correct algorithm
to apply the algorithm at the 'good' moment
to decide which algorithm to apply (sometimes
there are more than one, depending onconditions)
to know if a certain operation must be repeated
In short: we need a suitable Control Structu re
In 1966, two researchers, C. Bohn and G. Jacopini,demonstrated that any algorithm can be described
using only 3 control structures: sequence,
select ionand repeti t ion.
7/29/2019 Chapter2 ProblemSolving New
8/31
Principles of Programming - NI July 2005 8
Pseudocodes
A pseudocode is a semiformal, English-like
language with limited vocabulary that can be
used to design and describe algorithms.
Criteria of a good pseudocode:
Easy to understand, precise and clear
Gives the correct solution in all cases
Eventually ends
7/29/2019 Chapter2 ProblemSolving New
9/31
Principles of Programming - NI July 2005 9
Pseudocodes: The Sequence control structure
A series of steps or statements that are executed inthe order they are written in an algorithm.
The beginning and end of a block of statements canbe optionally marked with the keywords begin andend.
Example 1:
Begin
Read the birth date from the user.
Calculate the difference between the
birth date and todays date.Print the user age.
End
7/29/2019 Chapter2 ProblemSolving New
10/31
Principles of Programming - NI July 2005 10
Pseudocodes: The Selection control structure
Defines two courses of action depending on theoutcome of a condition. A condition is an expressionthat is, when computed, evaluated to either true orfalse.
The keyword used are ifand else.
Format:
if condition
then-part
elseelse-part
end_if
Example 2:
if age is greater than 55
print Pencenelse
print Kerja lagi
end_if
7/29/2019 Chapter2 ProblemSolving New
11/31
Principles of Programming - NI July 2005 11
Pseudocodes: The Selection control structure
Sometimes in certain situation, we may omit the else-part.
if number is odd number
print This is an odd number
end_if
Nested selection structure: basic selection structure thatcontains other if/else structure in its then-part or else-part.
if number is equal to 1print One
else if number is equal to 2print Two
else if number is equal to 3print Three
elseprint Other
end_if
Example 3
Example 4
7/29/2019 Chapter2 ProblemSolving New
12/31
Principles of Programming - NI July 2005 12
Pseudocodes: The Repetition control structure
Specifies a block of one or more statementsthat are repeatedly executed until a conditionis satisfied.
The keyword used is while.
Format:
while condition
loop-body
end_while
7/29/2019 Chapter2 ProblemSolving New
13/31
Principles of Programming - NI July 2005 13
Pseudocodes: The Repetition control structure
Example 5: Summing up 1 to 10
set cumulative sum to 0
set current number to 1
while current number is less or equal to 10
add the cumulative sum to current numberadd 1 to current number
end_while
print the value of cumulative sum
7/29/2019 Chapter2 ProblemSolving New
14/31
Principles of Programming - NI July 2005 14
Pseudocodes: The Repetition control structure
Example 9:while user still wants to playbeginSelect either to play on network or play against computerif play on network
create connection to remote machineplay game with connected computerElseselect missionplay game locallyend_ifAsk user whether he/she still wants to playendend_while
For readability, always use properindentation!!!
7/29/2019 Chapter2 ProblemSolving New
15/31
Principles of Programming - NI July 2005 15
Flowcharts
Flowcharts is a graph used to depict or showa step by step solution using symbols whichrepresent a task.
The symbols used consist of geometrical
shapes that are connected by flow lines.
It is an alternative to pseudocoding; whereasa pseudocode description is verbal, aflowchart is graphical in nature.
7/29/2019 Chapter2 ProblemSolving New
16/31
7/29/2019 Chapter2 ProblemSolving New
17/31
Principles of Programming - NI July 2005 17
Flowchart Symbols cont
Selection symbol - shows a selection process
for two-way selection.
Off-page connector - provides continuation
of a logical path on another page.
On-page connector - provides continuation
of logical path at another point in the same
page.
Flow lines - indicate the logical sequence of
execution steps in the algorithm.
7/29/2019 Chapter2 ProblemSolving New
18/31
Principles of Programming - NI July 2005 18
Flowchart sequence control structure
Statement 2
Statement 1
Statement 3
:
7/29/2019 Chapter2 ProblemSolving New
19/31
Principles of Programming - NI July 2005 19
Flowchart selection control structure
Condition
else-
statement(s)
then-
statement(s)
YesNo
7/29/2019 Chapter2 ProblemSolving New
20/31
Principles of Programming - NI July 2005 20
Flowchart repetition control structure
ConditionLoop
Statement(s)
yes
no
7/29/2019 Chapter2 ProblemSolving New
21/31
Principles of Programming - NI July 2005 21
Flowchart example 1
Begin
Read birth date
Calculate
Age = current year birth date
Display
age
End
7/29/2019 Chapter2 ProblemSolving New
22/31
Principles of Programming - NI July 2005 22
Flowchart example 2
Begin
Read age
End
Age > 55? NOYES
print Pencen print Kerja lagi
7/29/2019 Chapter2 ProblemSolving New
23/31
Principles of Programming - NI July 2005 23
Flowchart example 5
Begin
End
current_number
7/29/2019 Chapter2 ProblemSolving New
24/31
Principles of Programming - NI July 2005 24
Implementation
The process ofimplement ingan algorithm bywriting a computer program using a programminglanguage (for example, using C language)
The output of the program must be the solution
of the intended problemThe program must not do anything that it is notsupposed to do
(Think of those manyviruses, buf fer o verf lows,
t ro jan h orses, etc. that we experience almost daily. Allthese result from programs do ing morethan they
were intended to do)
7/29/2019 Chapter2 ProblemSolving New
25/31
Principles of Programming - NI July 2005 25
Testing and Verification
Program testing is the process of executing aprogram to demonstrate its correctness
Program verification is the process ofensuring that a program meets user-
requirement
After the program is compi led, we must runthe program and test/verify it with differentinputs before the program can be released tothe public or other users (or to the instructorof this class)
7/29/2019 Chapter2 ProblemSolving New
26/31
Principles of Programming - NI July 2005 26
Documentation
Contains details produced at all stages of theprog ram development cyc le.
Can be done in 2 ways:Writing comments between your line of codes
Creating a separate text file to explain theprogram
Important not only for other people to use ormodify your program, but also for you to
understand your own program after a long time(believe me, you will forget the details of yourown program after some time ...)
7/29/2019 Chapter2 ProblemSolving New
27/31
Principles of Programming - NI July 2005 27
Documentation cont
Documentation is so important because:You may return to this program in future to use thewhole of or a part of it again
Other programmer or end user will need someinformation about your program for reference ormaintenance
You may someday have to modify the program, or maydiscover some errors or weaknesses in your program
Although documentation is listed as the last
stage of software development method, it isactually an ongoing process which should bedone from the very beginning of the softwaredevelopment process.
7/29/2019 Chapter2 ProblemSolving New
28/31
Principles of Programming - NI July 2005 28
Volume calculation
Write a pseudocode and a flowchart for a Cprogram that read the value of the height,width and length of a box from the user andprint its volume.
7/29/2019 Chapter2 ProblemSolving New
29/31
Principles of Programming - NI July 2005 29
Calculating Electricity Bills
The unit for electricity usage is kWh. Fordomestic usage, the monthly rate is 21.8cents/unit for the first 200 unit, 25.8cents/unit for the next 800 units and 27.8cents/unit for each additional units. Giventhe amount of electricity units (in kWh) usedby a customer, calculate the amount ofmoney needs to be paid by the customer toTNB. A bill statement needs to be printed
out.Write a pseudocode and a flow chart tosolve the above problem.
7/29/2019 Chapter2 ProblemSolving New
30/31
Principles of Programming - NI July 2005 30
Sum of 1 to n
Write a pseudocode and a flowchart for aprogram that reads a positive integernandthen computes and prints the sum of allintegers between 1 and n.
7/29/2019 Chapter2 ProblemSolving New
31/31
Principles of Programming - NI July 2005 31
Summary
This chapter introduced the concept ofproblem solving-a process of transforming thedescription of a problem into a solution.
A commonly used method SDM which
consists of 6 steps
3 basic control structures : sequence,selection and repetition structures
Pseudocode vs. Flow chartT.H.E E.N.D