• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
6 problem solving with decisions
 

6 problem solving with decisions

on

  • 2,989 views

 

Statistics

Views

Total Views
2,989
Views on SlideShare
2,989
Embed Views
0

Actions

Likes
0
Downloads
81
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    6 problem solving with decisions 6 problem solving with decisions Document Transcript

    • Data Structures and Algorithms The Decision Logic Structure Some examples of conditional expressions are as follows : 1. A < B (A and B are the same the data type) 2. X + 5 >=Z (X and Z are numeric data) 3. E < 5 or F > 10 (E and F are numeric data) 4. DATAOK (DATAOK is a logical datum)Problem Solving with Decisions *Property of STI Page 1 of 40
    • Data Structures and Algorithms The Decision Logic StructureMultiple IF/THEN/ELSEThere are three types of decision logic you will use towrite algorithms for solutions consisting of more thanone decision. These types of decision logic include: · Straight-through logic · Positive logic · Negative LogicProblem Solving with Decisions *Property of STI Page 2 of 40
    • Data Structures and Algorithms The Decision Logic Structure@ Single Condition – Two Possible Actions or Sets of ActionsProblem Solving with Decisions *Property of STI Page 3 of 40
    • Data Structures and Algorithms The Decision Logic StructureStraight-through logic@ Means that all of the decisions are processed sequentially, one after the other.@ There is no ELSE part of the instruction; the FALSE branch always goes to the next decision, and the TRUE branch goes to the next decision after the instructions for the TRUE branch have been processed.Problem Solving with Decisions *Property of STI Page 4 of 40
    • Data Structures and Algorithms The Decision Logic StructurePositive Logic@ Allows the flow of the processing to continue through the module instead of processing succeeding decisions, once the resultant of a decision is true.@ Whenever the resultant is FALSE (the ELSE part of the decision), another decision in the sequence is processed until the resultant is TRUE, or there are no more decisions to process.@ At that time, the FALSE branch processes the remaining instructions.Problem Solving with Decisions *Property of STI Page 5 of 40
    • Data Structures and Algorithms The Decision Logic StructureNegative Logic@ similar to positive logic except that the flow of the processing continues through the module when the resultant of a decision is FALSE@ whenever the resultant is TRUE, another decision is processed until the resultant is FALSE, or there are no more decisions to process@ at that time, the TRUE branch processes the remaining instructions@ the hardest to use and understandProblem Solving with Decisions *Property of STI Page 6 of 40
    • Data Structures and Algorithms The Decision Logic StructureUsing Straight-through logicProblem: Find the amount to charge people of varying agesfor a food ticket. When the person is under 16, the charge isP7; when the person is 65 or over, the charge is P5; all othersare charged P10. The conditions are the following : AGE CHARGE AGE < 16 7 AGE >=16 and AGE < 65 10 AGE >=65 5Problem Solving with Decisions *Property of STI Page 7 of 40
    • Data Structures and Algorithms The Decision Logic StructureSolution : Algorithm Flowchart A IF AGE < 16 THEN T CHARGE = 7 IF T AGE < 16 IF AGE >= 16 AND AGE <65 THEN T CHARGE = 10 Charge = 7 IF AGE >= 65 F THEN T CHARGE = 5 IF AGE >= 16 T and AGE < 65 Charge = 10 F IF T AGE >= 65 Charge = 10 F BProblem Solving with Decisions *Property of STI Page 8 of 40
    • Data Structures and Algorithms The Decision Logic StructureProblem: Change the value of X to 0 when X becomesgreater than 100, and to change the value of Y to 0when Y becomes greater than 250 Algorithm Flowchart A IF X < 100 THEN IF T T X = 10 X > 100 IF Y > 250 X=0 THEN T F Y=0 IF T Y > 250 Y=0 F BProblem Solving with Decisions *Property of STI Page 9 of 40
    • Data Structures and Algorithms The Decision Logic StructureUsing Positive LogicProblem: Find the amount to charge people of varyingages for a food ticket. When the person is under 16,the charge is P7; when the person is 65 or over, thecharge is P5; all others are charged P10. The conditionsare the following : Algorithm IF AGE <16 THEN CHARGE = 7 ELSE IF AGE < 65 THEN CHARGE = 10 ELSE CHARGE = 5Problem Solving with Decisions *Property of STI Page 10 of 40
    • Data Structures and Algorithms The Decision Logic Structure Flowchart A F IF T AGE < 16 F IF T AGE < 65 CHARGE = 7 CHARGE = 5 CHARGE = 10 BProblem Solving with Decisions *Property of STI Page 11 of 40
    • Data Structures and Algorithms The Decision Logic StructureProblem: Calculate the commission rate for asalesperson, given the amount of sales. When thesalesperson has sold less than or equal to 2,000 worthof goods, the commission is 2%. When the sales total ismore than 2,000 and less than or equal to 4,000, thecommission is 4%. When sales total is more than 4,000and less than or equal to 6,000, the commission is 7%.When the person has sold more than 6,000 thecommission is 10%. The conditions are the following: SALES COMMISSION <=2000 .02 2001 – 4000 .04 4001 – 6000 .07 > 6000 .10Problem Solving with Decisions *Property of STI Page 12 of 40
    • Data Structures and Algorithms The Decision Logic Structure Solution 1: Algorithm IF SALES <= 2000 THEN COMMISSION = .02 T ELSE IF SALES <= 4000 THEN COMMISSION = .04 T ELSE IF SALES <= 6000 THEN F T COMMISSION = .07 F ELSE F COMMISSION = .1Problem Solving with Decisions *Property of STI Page 13 of 40
    • Data Structures and Algorithms The Decision Logic Structure Solution 1: Flowchart A F T IF SALES <=2000 F T COMMISSION = IF .02 SALES <=4000 COMMISSION F IF T = .04 SALES <=6000 COMMISSION COMMISSION = .1 = .07 BProblem Solving with Decisions *Property of STI Page 14 of 40
    • Data Structures and Algorithms The Decision Logic Structure Solution 1: Test 1. TEST FOR 3. TEST FOR SALES = 1500 SALES = 5500 IS SALES <= 2000 IS SALES <= 2000 TRUE FALSE COMMISSION = .02 IS SALES <= 4000 FALSE 2. TEST FOR IS SALES <= 6000 SALES = 3500 TRUE IS SALES <= 2000 COMMISSION = .07 FALSE IS SALES <= 4000 4. TEST FOR TRUE SALES = 7500 COMMISSION = .04 IS SALES <= 2000 FALSE IS SALES <= 4000 FALSE IS SALES <= 6000 TRUE COMMISSION = .1Problem Solving with Decisions *Property of STI Page 15 of 40
    • Data Structures and Algorithms The Decision Logic Structure Solution 2: Algorithm IF SALES > 6000 THEN COMMISSION = .1 T ELSE IF SALES > 4000 THEN COMMISSION = .07 T ELSE IF SALES > 2000 THEN F T COMMISSION = .04 F ELSE F COMMISSION = .02Problem Solving with Decisions *Property of STI Page 16 of 40
    • Data Structures and Algorithms The Decision Logic Structure Solution 2: Flowchart A F T IF SALES > 6000 F T COMMISSION = IF .1 SALES > 4000 COMMISSION F IF T = .07 SALES > 2000 COMMISSION COMMISSION = .02 = .04 BProblem Solving with Decisions *Property of STI Page 17 of 40
    • Data Structures and Algorithms The Decision Logic Structure Solution 2: Test 1. SALES = 1500 3. SALES = 5500 IS SALES > 6000 IS SALES > 6000 FALSE FALSE IS SALES > 4000 IS SALES > 4000 FALSE TRUE IS SALES > 2000 COMMISSION = .07 FALSE COMMISSION = .02 2. SALES = 3500 4. SALES = 7500 IS SALES > 6000 IS SALES > 6000 FALSE TRUE IS SALES > 4000 COMMISSION = .1 FALSE IS SALES > 2000 TRUE COMMISSION = .04Problem Solving with Decisions *Property of STI Page 18 of 40
    • Data Structures and Algorithms The Decision Logic StructureUsing Negative Logic @ Negative Logic is the hardest for most people to comprehend because they do not think in negative terms. @ In general when you use negative logic you are telling the computer to process another decision when the resultant condition is TRUE; @ If the resultant is FALSE, then the computer processes a consequent set of instructions and then continues processing the module.Problem Solving with Decisions *Property of STI Page 19 of 40
    • Data Structures and Algorithms The Decision Logic Structure Negative Logic Solution 1: Algorithm IF AGE >= 16 THEN IF AGE >= 65 THEN CHARGE = 5 ELSE CHARGE = 10 ELSE CHARGE = 7Problem Solving with Decisions *Property of STI Page 20 of 40
    • Data Structures and Algorithms The Decision Logic Structure Negative Logic Solution 1: Flowchart A F T IF AGE >= 16 F T CHARGE = 7 IF AGE >= 16 CHARGE = 10 CHARGE = 5 BProblem Solving with Decisions *Property of STI Page 21 of 40
    • Data Structures and Algorithms The Decision Logic StructureNegative Logic Solution 2: Algorithm IF SALES > 2000 THEN IF SALES > 4000 THEN IF SALES > 6000 THEN COMMISSION = .1 ELSE COMMISSION = .07 ELSE COMMISSION = .04 ELSE COMMISSION = .02Problem Solving with Decisions *Property of STI Page 22 of 40
    • Data Structures and Algorithms The Decision Logic StructureNegative Logic Solution 2: Flowchart A IF SALES > 2000 COMMISSION = .02 IF SALES > 4000 COMMISSION = .04 IF SALES > 6000 COMMISSION = COMMISSION = .07 .01 BProblem Solving with Decisions *Property of STI Page 23 of 40
    • Data Structures and Algorithms The Decision Logic StructureNegative Logic Solution 2: TEST 1. SALES = 1500 3. SALES = 5500 IS SALES > 2000 IS SALES > 2000 FALSE TRUE COMMISSION = .02 IS SALES > 4000 TRUE 2. SALES = 3500 IS SALES > 6000 IS SALES > 2000 FALSE TRUE COMMISSION = .1 IS SALES > 4000 FALSE 4. SALES = 7500 COMMISSION = .04 IS SALES > 2000 TRUE IS SALES > 4000 TRUE IS SALES > 6000 TRUE COMMISSION = .1Problem Solving with Decisions *Property of STI Page 24 of 40
    • Data Structures and Algorithms The Decision Logic StructureAnother Set-Up: Algorithm IF SALES <= 6000 THEN IF SALES <= 4000 THEN IF SALES <= 2000 THEN COMMISSION = .02 ELSE COMMISSION = .04 ELSE COMMISION = .07 ELSE COMMISSION = .1Problem Solving with Decisions *Property of STI Page 25 of 40
    • Data Structures and Algorithms The Decision Logic StructureAnother Set-Up: Flowchart A F T IF SALES <= 6000 CHARGE = .1 F T IF SALES <= 4000 COMMISSION = .07 F IF T SALES <= 2000 COMMISSION = COMMISSION = .04 .02 BProblem Solving with Decisions *Property of STI Page 26 of 40
    • Data Structures and Algorithms The Decision Logic StructureAnother Set-Up: Test 1. SALES = 1500 3. SALES = 5500 IS SALES <= 6000 IS SALES <= 6000 TRUE TRUE IS SALES <= 4000 IS SALES <= 4000 TRUE FALSE IS SALES <= 2000 COMMISSION = .07 TRUE COMMISSION = .02 2. SALES = 3500 4. SALES = 7500 IS SALES <= 6000 IS SALES <= 6000 TRUE FALSE IS SALES <= 4000 COMMISSION = .1 TRUE IS SALES <= 2000 FALSE COMMISSION = .04Problem Solving with Decisions *Property of STI Page 27 of 40
    • Data Structures and Algorithms The Decision Logic StructureLogic ConversionTo convert from positive logic to negative logic or viceversa, do the following : 1. Change all < to >= 2. Change all <= to > 3. Change all > to <= 4. Change all >= to < 5. Change all = to <> 6. Change all <> to = 7. Interchange all of the THEN set of instructions with the corresponding ELSE set of instructions.Problem Solving with Decisions *Property of STI Page 28 of 40
    • Data Structures and Algorithms The Decision Logic StructureConversion from Positive to Negative LogicProblem Solving with Decisions *Property of STI Page 29 of 40
    • Data Structures and Algorithms The Decision Logic StructureWhich Decision Logic?To analyze which type of decision logic would be mostefficient for a particular solution, answer the followingquestions:1. Which type would make the solution most readable?2. Which type would make the solution the easiest to maintain or change?3. Which would require the fewest test when you don’t know anything about the data?4. Which would require the fewest test when you’re given some data?Problem Solving with Decisions *Property of STI Page 30 of 40
    • Data Structures and Algorithms The Decision Logic StructureFour ways to Design a Set of ConditionsProblem Solving with Decisions *Property of STI Page 31 of 40
    • Data Structures and Algorithms The Decision Logic StructureDecision TablesA decision table consists of four parts: @ The conditions. @ The actions. @ The combinations of TRUE and FALSE for the conditions. @ The action to be taken or the consequences for each combination of conditions.The four steps to develop a flowchart from the decision tableare : @ Draw all decisions in flowchart form. @ Compare the true and false sides of each decision, starting with the first one. @ Eliminate any decisions that have the same instructions on both the true and false sides, keeping the true consequence or action. @ Redraw the flowchart.Problem Solving with Decisions *Property of STI Page 32 of 40
    • Data Structures and Algorithms The Decision Logic StructureDecision Table FormatProblem Solving with Decisions *Property of STI Page 33 of 40
    • Data Structures and Algorithms The Decision Logic StructureProblem Solving with Decisions *Property of STI Page 34 of 40
    • Data Structures and Algorithms The Decision Logic Structure Elimination of ConditionsProblem Solving with Decisions *Property of STI Page 35 of 40
    • Data Structures and Algorithms The Decision Logic StructureFinal FlowchartProblem Solving with Decisions *Property of STI Page 36 of 40
    • Data Structures and Algorithms The Decision Logic StructureDecision TableProblem Solving with Decisions *Property of STI Page 37 of 40
    • Data Structures and Algorithms The Decision Logic Structure Starting FlowchartProblem Solving with Decisions *Property of STI Page 38 of 40
    • Data Structures and Algorithms The Decision Logic Structure Elimination of ConditionProblem Solving with Decisions *Property of STI Page 39 of 40
    • Data Structures and Algorithms The Decision Logic Structure Final FlowchartProblem Solving with Decisions *Property of STI Page 40 of 40