6 problem solving with decisions

40
Problem Solving with Decisions *Property of STI Page 1 of 40 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)

description

 

Transcript of 6 problem solving with decisions

  • 1. Data Structures and AlgorithmsThe DecisionLogic 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

2. 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 3. 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 4. Data Structures and Algorithms The Decision Logic StructureStraight-through logic@ Means that all of the decisions are processedsequentially, one after the other.@ There is no ELSE part of the instruction; the FALSEbranch always goes to the next decision, and theTRUE branch goes to the next decision after theinstructions for the TRUE branch have beenprocessed.Problem Solving with Decisions*Property of STI Page 4 of 40 5. Data Structures and Algorithms The Decision Logic StructurePositive Logic@ Allows the flow of the processing to continuethrough the module instead of processingsucceeding decisions, once the resultant of adecision is true.@ Whenever the resultant is FALSE (the ELSE part ofthe decision), another decision in the sequence isprocessed until the resultant is TRUE, or there areno more decisions to process.@ At that time, the FALSE branch processes theremaining instructions.Problem Solving with Decisions*Property of STI Page 5 of 40 6. 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 7. 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 < 6510 AGE >=65 5Problem Solving with Decisions*Property of STI Page 7 of 40 8. Data Structures and Algorithms The Decision Logic StructureSolution : Algorithm FlowchartA IF AGE < 16 THEN T CHARGE = 7IFTAGE < 16 IF AGE >= 16 AND AGE = 65 F THEN T CHARGE = 5IFAGE >= 16 TandAGE < 65 Charge = 10F IF TAGE >= 65 Charge = 10 FBProblem Solving with Decisions*Property of STI Page 8 of 40 9. 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 AlgorithmFlowchart A IF X < 100 THEN IF T T X = 10X > 100 IF Y > 250X=0THEN T F Y=0IF T Y > 250Y=0 F BProblem Solving with Decisions*Property of STI Page 9 of 40 10. Data Structures and AlgorithmsThe DecisionLogic 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 THENIF AGE >= 65 THENCHARGE = 5 ELSECHARGE = 10 ELSECHARGE = 7Problem Solving with Decisions*Property of STIPage 20 of 40 21. Data Structures and AlgorithmsThe DecisionLogic StructureNegative Logic Solution 1: FlowchartA FTIF AGE >= 16FT CHARGE = 7IFAGE >= 16CHARGE = 10CHARGE = 5 BProblem Solving with Decisions *Property of STI Page 21 of 40 22. Data Structures and Algorithms The Decision Logic StructureNegative Logic Solution 2: Algorithm IF SALES > 2000THEN IF SALES > 4000THEN IF SALES > 6000THEN COMMISSION = .1ELSE COMMISSION = .07 ELSECOMMISSION = .04ELSE COMMISSION = .02Problem Solving with Decisions *Property of STI Page 22 of 40 23. Data Structures and Algorithms The Decision Logic StructureNegative Logic Solution 2: FlowchartA IF SALES > 2000 COMMISSION = .02IFSALES > 4000 COMMISSION = .04 IF SALES > 6000COMMISSION =COMMISSION =.07 .01 BProblem Solving with Decisions*Property of STIPage 23 of 40 24. Data Structures and Algorithms The Decision Logic StructureNegative Logic Solution 2: TEST 1. SALES = 1500 3. SALES = 5500IS SALES > 2000 IS SALES > 2000FALSE TRUECOMMISSION = .02IS SALES > 4000TRUE 2. SALES = 3500IS SALES > 6000IS SALES > 2000 FALSETRUECOMMISSION = .1IS SALES > 4000FALSE4. SALES = 7500COMMISSION = .04IS SALES > 2000TRUEIS SALES > 4000TRUEIS SALES > 6000TRUECOMMISSION = .1Problem Solving with Decisions*Property of STIPage 24 of 40 25. Data Structures and AlgorithmsThe DecisionLogic StructureAnother Set-Up: Algorithm IF SALES