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.
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
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.
6 Pseudocoding The 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 learn 3. Produce simple, English-link narrative notation 4. Be capable of describing all algorithms
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
8 The Selection Control Structure define two courses of action, depending on the outcome of a condition. if condition then-part else else-part end_if the else-part may be missing.
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
10 if status is equal to 1 print “Single” end_if if status is equal to 2 print “Married filing jointly” end_if
11 The Repetition Control Structure specifies 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
12 Print “Enter taxable income; should be greater than or equal to $50,000.00” read income while income is less than begin print “Enter taxable income; should be greater than or equal to $50,000.00” read income end end_while
13 Conventions for Pseudocoding 1.Each pseudocode statement includes keywords 2.Each pseudocode statement should be written on a separate line. 3.Statements in a sequence structure can be grouped into a block 4.For the selection control structure, use an if/else statement. 5.For the repetition control structure, use the while statement 6.All words in a pseudocode statement must be unambiguous and as easy as possible for nonprogrammers to understand.
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.
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 5 set correctstatusInput to “yes” end_if end end_while Figure 2.7 Final refinement of read and verify filing status
16 if filingStatus is 1 compute tam = * (income – ) else if filingStatus is 2 if income is less than or equal to 82,150 compute tax = * (income – ) else compute tax = * (income – ) end_if else if filingStatus is 3 compute tax = * (income – ) else if status is 4 if income is less than or equal to compute tax = * (income – ) else compute tax = * (income – ) end_if Figure 2.8 Final refinement of compute tax (continued)
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_if print tax Figure 2.9 Final refinement of print income, filing status, and tax
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
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.
20
21
22
23
24
IMPLEMENTATION translate each step of the algorithm into a statement in that particular language Programming Errors debugging three 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.
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.
PROGRAM DOCUMENTATION 1. A concise requirements specification 2. Descriptions of problem inputs, expected outputs, constraints, and applicable formula. 3. A pseudocode or flowchart for its algorithm 4. A source program listing 5. A hard copy of a sample test run of the program 6. A user’s guide explaining to nonprogrammer users how the program should be used (optional)