problem solving n analysis

39

Upload: yi-chin

Post on 07-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 1/39

Page 2: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 2/39

Problem Solving  Is the process of transforming the description of a problem

into the solution of that problem by using our knowledge ofthe problem domain and relying on our ability to select anduse appropriate problem-solving strategies, techniques, andtools."

  Using computers 

2ms_2010

Page 3: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 3/39

Problem Solving  Computers as a tool in problem solving:"

  Extensive input"

  Extensive output"

  Method of solution is too complicated to

implement manually"

  If done manually, it takes long time to solve"

  Expect to use the same method of solution infuture to solve the same problem with different

inputs."

3ms_2010

Page 4: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 4/39

Problem Solving  Software Development method:

1.  Requirement Specification"2.  Analysis"3.  Design"4.  Implementation"5.  Testing and verification"6.  Documentation"

4ms_2010

Page 5: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 5/39

1. Requirement Specification"  Understanding what the problem is"

  What is needed to solve it"

  What the solution should provide"

  If there is constraints and specialconditions"

  How precisely we can define a problem depends on our degree

of familiarity with the problem domain"

5ms_2010

Page 6: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 6/39

1. Requirement Specification"Example:"

A taxpayer can determineincome tax by using tax table if

the taxable income is less thanor equal to $50,000. However,if exceeds, use the tax rateschedules. Tax rate schedules

depend on filing status."

6ms_2010

Page 7: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 7/39

1. Requirement Specification"Filing Status Over But Not Over Tax

Single 49,300 - 11,158 + 31% of amt over 49,300

Married filing jointly 34,000

82,150

82,150

-

5,100 + 28% of amt over 34,000

18,582 + 31% of amt over 82,150

Married filing separately 41,075 - 9,291 + 31% of amt over 41,075

Head of household 27,300

70,450

70,450

-

4,095 + 28% of amt over 27,300

16,177 + 31% of amt over 70,450

7

Taxable Income

 Prompt user to enter taxable amount and read amount

 Display menu filing status code

 Read status code

 Compute tax

 Output results

ms_2010

Page 8: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 8/39

2. Analysis"☻  Identify:"  Inputs to the problem"

  Outputs expected from the solution"

  Constraints"

  Formulas"

8ms_2010

Page 9: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 9/39

2. Analysis"Example"

  Inputs    – taxable income and filingstatus, interactive input."

  Outputs – filing menu and final output :

taxable income, filing status, andcomputed income tax."

  Constraints – should not accept less50,000, filing code should be 1,2,3,4."

 

Formulas – tax rate schedule."

9ms_2010

Page 10: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 10/39

3. Design

  Algorithm

"Is a sequence of a finite number of steps arranged in a specificlogical order, which, when executed, produce the solution for aproblem."

  Unambiguousness"

  Generality"

  Correctness"

  Finiteness"

10ms_2010

Page 11: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 11/39

3. Design

  Representing Algorithm"

11

 pseudocodewords

descriptions

flowchart

graphical

descriptions 

ms_2010

Page 12: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 12/39

3. Design

  Representing Algorithm"

  pseudocode "

  Is a semiformal, English-like language with alimited vocabulary that can be used to design

and describe algorithms"

  pseudocode language must:"

  Limited vocabulary"

  Simple and easy to learn"

  Capable of describing complex problems"

12ms_2010

Page 13: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 13/39

3. Design

  Pseudocode language has 3 control structures:"

1)"

Sequence"

2) "Selection "

3) "Repetition"

13

Is a series of steps or statements that are executed

in the order in which they are written in an algorithm.

Define two courses of action, depending on the outcomeof a condition. A condition is an expression that, when

evaluated, computes to either true or false.

Specifies a block of one or more statements that are

repeatedly executed until a condition is satisfied.

ms_2010

Page 14: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 14/39

3. Design

Sequence Control Structure"

14

Is a series of steps or statements that are executed

in the order in which they are written in an algorithm.

 beginread taxable income

read filing status

compute income tax

 print income tax

end

ms_2010

Page 15: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 15/39

3. Design

Selection Control Structure"

15

if conditionthen-part

else

else-part

end_if 

Define two courses of action, depending on the outcome

of a condition. A condition is an expression that, when

evaluated, computes to either true or false.

if conditionthen-part

end_if 

if status is equal to 1

 print “Single”end_if 

ms_2010

Page 16: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 16/39

3. Design

Selection Control Structure"

16

if income less than or equal to 82,150

 begin

compute tax = 5100 + 0.28 * (income – 34000)

 print “ Tax rate is 28%”

end

else begin

compute tax = 18582 + 0.31 * (income – 82150)

 print “ Tax rate is 31%”

end

end_if 

ms_2010

Page 17: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 17/39

3. Design

Nested Selection Control Structure"

17

Is a basic selection structure that contains other if/else structure in its then-part or else-part 

if status is equal to 1

 print “single”

else if status is equal to 2

 print “married filing jointly”else if status is equal to 3

 print “married filing separately”else if status is equal to 4

 print “head of household”

else

 print “error in status”

end_if 

if status is equal to 1

 print “single”

end_if 

if status is equal to 2 print “married filing jointly”

end_if 

if status is equal to 3

 print “married filing separately”

end_if if status is equal to 4

 print “head of household”if status is less than 1

 print “error in status”

end_if 

ms_2010

Page 18: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 18/39

3. Design

Repetition Control Structure"

18

while condition

loop-body

end_while

Specifies a block of one or more statements that are

repeatedly executed until a condition is satisfied.

 Define loops using while

read income

while income is less than 50000

 begin

 print”please enter taxable income”

read incomeend

end_while

ms_2010

Page 19: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 19/39

Pseudocode

  Conventions:"  Includes keywords describing operations and operands;

compute, set, initialize, add, begin, end, while, end- while, if, end-if, then, else, print, read.!

  Each statement in one line"

  Enclose block statement"

  Indent continuous statement"

  Unambiguous and easy to understand"

19

if nombor is 10

 begin

 print “The number”

 print “is Ten”

end

ms_2010

Page 20: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 20/39

3. Design

  Representing Algorithm"

  Flowchart"  Is a graph consisting of geometrical shapes

that are connected by flow lines."

  The shapes represent types of statements"

  Details statements is written inside the shapes"

20ms_2010

Page 21: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 21/39

Flowchart

  Notations:

Syntax Semantic

21

begin/end

process

input/output

ms_2010

Page 22: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 22/39

Flowchart

  Notation

Syntax Semantic

22

condition

continue

flow 

ms_2010

Page 23: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 23/39

Pseudocode

begin

Statement_1

Statement_2

Statement_n

end

Flowchart

23

Flowchart Sequence structure 

Statement_1 Statement_nStatement_2

ms_2010

Page 24: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 24/39

Pseudocode

if condition

then-part

else

else-part

end_if

Flowchart

24

Flowchart Selection structure 

else-part then-part

condition truefalse

ms_2010

Page 25: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 25/39

Pseudocode

if condition

then-part

end_if

Flowchart

25

Flowchart Selection structure (missing else-part) 

then-part

condition truefalse

ms_2010

Page 26: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 26/39

Pseudocode

while condition

loop-body

end_while

Flowchart

26

Flowchart Repetition structure (0 or more) 

loop-bodycondition

true

false

ms_2010

Page 27: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 27/39

Pseudocode

begin

loop-body

while condition

end_while

Flowchart

27

Flowchart Repetition structure (at least once) 

loop-body

condition

true

false

ms_2010

Page 28: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 28/39

Pseudocodestart

read 2 numbers

compute total

compute multiplication

compute average

print results

end

Flowchart

28

Examples:  

Total = no1 + no2

Multi = no1 * no2

Ave = Total/2

 begin

Read no1

Read no2

end

Print results

ms_2010

Page 29: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 29/39

Pseudocode

begin

read mark

compute grade

if grade >= 2.0

print pass

end

Flowchart

29

example 

compute grade

grade >= 2.0

true

false

read mark 

“pass”

ms_2010

Page 30: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 30/39

Pseudocode

begin

read mark

compute grade

if grade >= 2.0

print pass

end

Flowchart

30

example 

compute grade

grade >= 2.0

true

false

read mark 

“pass”

ms_2010

Page 31: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 31/39

31

True

 print total, average

count < 5

Set count 0

Set total 0Set average 0

 begin

total = total + mark 

read mark 

count = count + 1

false

average = total/count

end

ms_2010

Page 32: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 32/39

Repetition  Two ways to control repetition 

  Sentinel value 

  Unique value

  Unknown number of looping

  counter 

  Control the number of looping

32ms_2010

Page 33: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 33/39

Pseudocode - sentinel begin 

read StuNum 

while (StuNum not equal–999) 

 begin_while 

read test_mark 

read assign_mark 

compute coursework 

read the next StuNum 

end_while 

end  

33ms_2010

Page 34: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 34/39

Pseudocode - Counter  begin 

StuTot←

0 read StuNum 

while (StuTot <= 60) 

 begin_while 

read test_mark 

read assign_mark 

compute coursework

accumulate StuTot

read the next StuNum 

end_while 

end 

34ms_2010

Page 35: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 35/39

Top-Down StepwiseRefinement 

  Structure Chart

  Use divide-and-conquer strategy to split the problem intosmaller subproblems

  Refine the subproblems individually

  Combine all refinements

35ms_2010

Page 36: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 36/39

4. Implementation 

  Translating each step in algorithm into computerlanguage computer program"

  Computer program"

  is a sequence of finite number of statementsexpressed in a programming language in a specific

logical order that, when executed, produce thesolution for a problem."

  Programming Errors"

  Design errors"

  Syntax Errors"

 

Run-time errors"

36ms_2010

Page 37: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 37/39

5. Testing and verification

  Testing"

  Is the process of executing a program todemonstrate its correctness."

  Use test data"

  Verification"  Is the process of ensuring that a program

meets user requirements."

37ms_2010

Page 38: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 38/39

6. Documentation 

  Why?"

  You are likely to return to this program in future"

  In your absence, easier for others to refer to"

  Others may need to maintain the program"

  Easier to refer for corrections"

38ms_2010

Page 39: Problem Solving n Analysis

8/3/2019 Problem Solving n Analysis

http://slidepdf.com/reader/full/problem-solving-n-analysis 39/39

6. Documentation 

  Consists of:"

  Requirements specification"

  Descriptions of I/o, constraints and formula"

  Pseudocode"

  Source program"

  Sample test"

  Userʼs guide"

39ms_2010