27
1 Chapter 2 Problem Solving Techniques

1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

  • View
    219

  • Download
    2

Embed Size (px)

Citation preview

Page 1: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

1

Chapter 2

Problem Solving Techniques

Page 2: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

2

2.1 INTRODUCTION

2.2 PROBLEM SOLVING

2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

Page 3: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

3

consists of the following steps :

1. Requirements specification

2. Analysis

3. Design

4. Implementation

5. Testing and verification

6. Documentation

divide and conquer split the problem into several simpler subproblems, solve each individually, and then combine these solutions.

Page 4: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

4

2.4 REQUIREMENTS SPECIFICATION

understanding exactly what the problem is, what is needed to solve it, what the solution should provide.

2.5 ANALYSIS

identify the following :

1. Inputs to the problem

2. Outputs expected from the solution

3. Any special constraints or conditions

4. Formulas or equations to be used

Page 5: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

5

2.6 DESIGN AND REPRESENTATION OF ALGORITHMS

An algorithm is a sequence of a finite number of steps arranged in a specific logical order, which, when executed, produce the solution for a problem.

An algorithm is a procedure that takes a set of values as input and transforms them into a set of values as output

In practice we expect algorithms to be efficient.

Page 6: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

6

PseudocodingThe main purpose of a pseudocode language is to define the procedural logic of an algorithm requirements:1. Have a limited vocabulary 2. Be easy to learn3. Produce simple, English-link narrative

notation4. Be capable of describing all algorithms

Page 7: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

7

The Sequence Control Structure

is a series of steps or statements that are executed in the order in which they are written in an algorithm

read taxable income

read filing status

compute income tax

print income tax

Figure 2.1 A sequence control structure

Page 8: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

8

The Selection Control Structuredefine two courses of action, depending on the outcome of a condition.

if conditionthen-part

elseelse-part

end_if

the else-part may be missing.

Page 9: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

9

A nested selection structure contains other if/else structures 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”

end_if

Page 10: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

10

if status is equal to 1

print “Single”

end_if

if status is equal to 2

print “Married filing jointly”

end_if

Page 11: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

11

The Repetition Control Structurespecifies a block of one of one or more statements that are repeatedly executed until a condition is satisfied.

while condition loop-body

end_while

loop-body is a single statement or a block of statements

Page 12: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

12

Print “Enter taxable income; should be greater than or equal to $50,000.00” read income

while income is less than 50000.00

begin

print “Enter taxable income; should be greater than or equal to $50,000.00”

read income

end

end_while

Page 13: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

13

Conventions for Pseudocoding1. Each pseudocode statement includes keywords2. Each pseudocode statement should be written on

a separate line.3. Statements in a sequence structure can be groupe

d into a block 4. For the selection control structure, use an if/else

statement.5. For the repetition control structure, use the while

statement6. All words in a pseudocode statement must be un

ambiguous and as easy as possible for nonprogrammers to understand.

Page 14: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

14

Structure chart, as in figure 2.6. This chart indicates that the original problem is solved if the four subproblems at the lover level of hierarchy are solved from left to right.

Page 15: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

15

set correctStatusInput to “no”print “FILING STATUS MENU:”print “Single ----1”print “Married filing jointly ----2”print “Married filing separately ----3”print “Head of household ----4”while correctStatusInput is equal to “no”

begin print “Enter filing status; should be between 1 and 4:” read filingStatus if filingStatus is greater than 0

if filingStatus is less than 5set correctstatusInput to “yes”

end_if end_ifend

end_while

Figure 2.7 Final refinement of read and verify filing status

Page 16: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

16

if filingStatus is 1

compute tam = 11158.50 + 0.31 * (income – 49300.00)else if filingStatus is 2

if income is less than or equal to 82,150 compute tax = 5100.00 + 0.28 * (income – 34000.00)else compute tax = 18582.00 + 0.31 * (income – 82150.00)end_if

else if filingStatus is 3compute tax = 9291.00 + 0.31 * (income – 41075.00)

else if status is 4if income is less than or equal to 70450.00 compute tax = 4095.00 + 0.28 * (income – 27300.00)else compute tax = 16177.00 + 0.31 * (income – 70450.00)end_if

end_if

Figure 2.8 Final refinement of compute tax (continued)

Page 17: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

17

print income if filingStatus is equal to 1

print “Single”else if filingStatus is equal to 2

print “Married filing jointly”else if filingStatus is equal to 3

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

print “Head of household”end_ifprint tax

Figure 2.9 Final refinement of print income, filing status, and tax

Page 18: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

18

Algorithm design technique

1. Use the divide-and-conquer strategy to split the original problem into a series of subproblems.

2. Consider each subproblem separately and further split them into subproblems, until no further refinement is possible. Stop the process when you have a pseudocode that can be directly translated into a C program.

3. Combine all final refinements

Page 19: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

19

Flowcharting alternative to pseudocoding;flowchart is a graph consisting of geometrical shapes

the geometrical shapes in a flowchart represent the types of statements in an algorithm. The details of statements are written inside the shapes. The flow lines show the order in which the statements of an algorithm are executed. Two geometrical shapes used in flowcharting are the rectangle, which represents processes such as computations, assignments, and initializations, and the diamond-shaped box, which stands for the keyword if in pseudocode, with the if condition written inside the box.

Page 20: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

20

Page 21: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

21

Page 22: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

22

Page 23: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

23

Page 24: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

24

Page 25: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

25

2.7 IMPLEMENTATIONtranslate each step of the algorithm into a statement in that particular language

Programming Errorsdebuggingthree types of programming errors :1. design errors : occur during the analysis,design, and implementation phases.2. Syntax error : detected by the compiler during

the compilation process.3. Run-time errors : detected by the computer while

your program is being executed. Divide a number by zero.

Page 26: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

26

2.8 TESTING AND VERIFICATION

A program must be tested using a sufficiently large sample of care fully designed test data sets such that every logical path in the program is traversed at least once.

Page 27: 1 Chapter 2 Problem Solving Techniques. 2 2.1 INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD

27

2.9 PROGRAM DOCUMENTATION

1. A concise requirements specification2. Descriptions of problem inputs, expected outputs, constraints, and applicable formula.3. A pseudocode or flowchart for its algorithm4. A source program listing5. A hard copy of a sample test run of the program6. A user’s guide explaining to nonprogrammer

users how the program should be used (optional)