Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured thinking Part 1: 1. Introduction 2. Structure Diagram 3. Comparison Part 2: 4. Examples 5. Rules 7. More examples Part 3: 8. Structure - pseudocode.

Similar presentations


Presentation on theme: "Structured thinking Part 1: 1. Introduction 2. Structure Diagram 3. Comparison Part 2: 4. Examples 5. Rules 7. More examples Part 3: 8. Structure - pseudocode."— Presentation transcript:

1

2 Structured thinking

3 Part 1: 1. Introduction 2. Structure Diagram 3. Comparison Part 2: 4. Examples 5. Rules 7. More examples Part 3: 8. Structure - pseudocode 9. Pseudocode - C code 10. EndContents

4 Morning routine Structure diagram ”Morningwhile has gold in mouthd” Get dressed Eat breakfastGo to school Time Division

5 Sequence (order in time) Get dressed Eat breakfastGo to school Morning routine 8:00 am8:02 am8:05 am Time Sequence Sequence parts

6 Structure diagram ”Morningwhile has gold in mouthd” Find clothesPut clothes on Morning routine Get dressedEat breakfastGo to school Division Time Take from wash basket Ask girlfriend where clothes are

7 Selection BluePinkWhite What colour on your socks? If blue exists If blue doesn’t exist, but pink does Else Selection Selection parts

8 Structure diagram ”Morningwhile has gold in mouthd” Many glasses of beer One glass of beer Division Time Morning routine Take from wash basket Ask girlfriend where clothes are Find clothesPut clothes on Get dressedEat breakfastGo to school

9 Iteration (repetition) Many glasses of beer One beer Drink beer (lightbeer) As long as you’re thirsty Iteration Iteration part

10 Structure diagram ”Morningwhile has gold in mouthd” Division Time Take a cabRun One step Morning routine Take from wash basket Ask girlfriend where clothes are Find clothesPut clothes on Get dressedEat breakfastGo to school Many glasses of beer One glass of beer

11 Flow chart Move foot one step Run Go to school Eat one pancake Eat breakfast Put on the clothes Ask girl/boyfriend Get from laundry basket Search clothes Get dressed Morning route IF you find basket WHILE pancakes <> 0 Take a cab IF money = 0 ELSE WHILE distance to school <> 0

12 Summary l A problem can be divided into a number of smaller problems. l When programming, it can sometimes be difficult to know where to start. This is where you use a structure diagram.

13 The end part 1

14 Examples Rules More examples Program structure

15 Crossing A B Imagine driving in car B, just behind another car A. You come to a crossing. How will you know which direction it will chose? Car A has to make a selection

16 Crossing a selection of directions How can you see in which direction the car in front of you will choose? Crossing Green signal STOPDRIVE Red signal Left blinkerRight blinkerNo blinker Drive leftStraight aheadDrive right Selection

17 Marriage - a selection? Marry girl Young man´s future choice not JSP

18 Selectionparts Young man´s future choice A selection must contain at least two selection components. If It only contains one there is no selection. Marry girl Be free!! Like this!

19 Al Bundy? Young man´s future choice We know the end…? Peg.. - make dinner! Marry girl It is however possible to leave a selection component empty.

20 Example of an iteration InitiateInput name etc PersonsTerminate Label Person * Until name = ”end” Status quoLabelsInput name etc Print Label * Until N > 10 Iterations Persons Labels

21 Rules of Christmas Checks his slade Santa Claus X-mas day Gives one gift * While gifts not 0 not JSP

22 Santa changed his mind Parts connected to the same component must be of similar type Santa Claus X-mas day Checks his slade First Hands out gifts Last This is CORRECT!! Gives one gift While gifts not 0 *

23 Paint house - an iteration Paint House Get paint * While painter not satisfied with color Paint with Brush * While entire house not painted not JSP

24 Now you can begin to paint Get paintBegin to paint Paint House An iteration can only contain one repeated component Get one can of paint Paint with Brush * * While painter not satisfied with color While entire house not painted Iterations Iteration components OK !

25 Example of JSP InputCalculationPrint Electricity bill Tariff ATariff B Tariff = A Tariff = B 16 A20 A25 A Fuse = 16 = 25 = 20 16 A20 A25 A = 20 = 25 = 16

26 Program structure Transfering the problem into code Split the problem into smaller problems Make a structure of the problems (indata/outdata) Write down an operationlist and the conditions ?

27 What is an operation? An operation describes WHAT to do with all the data that exist in the structure. An operation can for instance: read information, calculate, sort, seek, print, etc.. Operations are placed in the structure with a symbol

28 Radius and Area Program Example: Construct a programstructure to a program that calculates and prints the area of a circle Operationlist - what do we need? We realize there is three important operations we need to complete this task. First, we need to put in (input) the radius of the circle Second, calculate the area Third, print (output) the answer

29 And.. 1 3 2 We have three operations listed and placed on the structure diagramPRESTO! Operationlist 1. Radius of the circle 2. Calculate circle area 3. Print circle area Radius and Area Program

30 Operationlist Operationlist A structure to a program that calculates the area or the circumference to a rectangle 1. Read length of rectangle 2. Read width of rectangle Data LengthWidth Area or circumf. AreaCirumf. 3. Read selection (area/circumference) 4. Calculate area of rectangle 5. Print area of rectangle 6. Calculate circumference 7. Print circumference of rectangle 12 3 4 5 6 7

31 Program structure Headline of table Pair of numbers Table Program Until x = 100 * Operationlist 1. Print headline of table 2. Input value of x 3. Print x value 4. Calculate value if y 5. Print y value Conditionlist (C 1) Until x = 100 1 2 34 5 2 A conditionlist contains all the conditions used in the structure

32 Program structure Transfering the problem into code Split the problem into smaller problems Make a structure of the problems (indata/outdata) Write an operationslist and the conditions Pseudocode can be used ?

33 What is pseudocode? Pseudocode is a combination between ordinary language and real programcode The pseudocode includes the sequences, selections, iterations, conditions and operations placed in the structurediagram Pseudocode can be used to simplify the structured problem before it becomes real programcode

34 Milk the cow - Pseudocode Peasant needs money

35 Milk the cow - Pseudocode Peasant needs money Milk the cow Operationlist Conditionlist 1. Input peasant_tired value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value 1. If peasant_tired = yes (sel bm) 2. While milk = exist 3. While milk = exist 1 By machine By hand IF peasant_tired = yes ELSE Squeeze * * WHILE milk = exist WHILE milk = exist 4567 23

36 Milk the cow - Pseudocode Peasant needs money Milk the cow By hand ELSE Squeeze * WHILE milk = exist 67 IFneighbour_ home = yes Operationlist Conditionlist 8. Input neighbour_home value 9. Read selection (neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 4. If neighbour_home = yes Sell milk To neighboursTo village ELSE 1012 11 12 23 9 8

37 Milk the cow - Pseudocode Peasant needs Operationlist Conditionlist 13. Buy clothes 14. Read money value 5. While money = exist Spend money Buy clothes for wife * WHILE money = exist To neighboursTo village ELSE money If neighbour _home Sell milk 1012 11 14 13 9 8

38 Milk the cow - Pseudocode Peasant needs money Milk the cow Sell milk By machine By hand IF peasant_tired = yes ELSE Squeeze * * WHILE milk = exist WHILE milk = exist IF neighbour_ho me = yes To neighboursTo village ELSE Spend money Buy clothes for wife * WHILE money = exist

39 Pseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 3. If neighbour_home = yes 4. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILE

40 Pseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 4. If neighbour_home = yes 5. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILE

41 Pseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 4. If neighbour_home = yes 5. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILE

42 Pseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 4. If neighbour_home = yes 5. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILE

43 Pseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 3. If neighbour_home = yes 4. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILE

44 Pseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 3. If neighbour_home = yes 4. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILE

45 Pseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 3. If neighbour_home = yes 4. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILE

46 Pseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 3. If neighbour_home = yes 4. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILE

47 Pseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 3. If neighbour_home = yes 4. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILE

48 PEASANT NEEDS MONEY Input peasant value IF peasant_tired = yes milk with machine ELSE milk by hand ENDIF Input milk value WHILE milk = exist Squeeze Read milk value ENDWHILE Input neighbour_home IF neighbour_home = yes sell milk to neighbours ELSE sell milk in village ENDIF Input money value WHILE money = exist Buy clothes for wife Read money value ENDWHILEPseudocode 1. If peasant_tired = yes (sel bm) 2. While milk = exist 3. If neighbour_home = yes 4. While money = exist Conditionlist 13. Buy clothes 14. Read money value 8. Input neighbour_home value 9. Read selection(neighbour/village) 10. Sell to neighbours 11. Sell to village 12. Input money value 1. Input peasant value 2. Read selection (hand/machine) 3. Input milk value 4. Squeeze 5. Read milk value 6. Squeeze 7. Read milk value Operationlist OK! How about some real programcoding?

49 The end part 2

50 Structure diagram Structure diagram to pseudocode Pseudocode to C programming Solving 2nd degree equations C

51 Program structure y = ax +bx +c 2 2nd degree equation 0 = ax +bx +c 2 D

52 Solving 2nd degree equations Program structure 0 = ax +bx +c 2 Information 2nd degree equation

53 Solving 2nd degree equations Program structure 0 = ax +bx +c 2 Solve equation? One equation * While answer  n or N Information 2nd degree equation

54 Solving 2nd degree equations Program structure 0 = ax +bx +c 2 Possible solution Solve equation? One equation * While answer  n or N Information Constant aConstant cConstant b 2nd degree equation

55 Solving 2nd degree equations Program structure 0= ax +bx +c 2 Possible solution Solve equation? One equation * While answer  n or N Information Constant aConstant cConstant b If D  0 SolutionNo real solution D = b²/(4a²)-c/a 2nd degree equation

56 Solving 2nd degree equations Program structure 0 = ax +bx +c 2 Possible solution Solve equation? One equation * While answer  n or N Information Constant aConstant cConstant b If D  0 SolutionNo real solution One solution If D  0 Two solutions D = b²/(4a²)-c/a 2nd degree equation

57 Solving 2nd degree equations Program structure 0 = ax +bx +c 2 Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions D = b²/(4a²)-c/a

58 Solving equations Pseudo coding Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Rotate and Flip

59 Solving equations Pseudo coding Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Rotate and Flip Division Time

60 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

61 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

62 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

63 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

64 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

65 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

66 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

67 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

68 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

69 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

70 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

71 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

72 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

73 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

74 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

75 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

76 Solving equations Pseudo coding Solving 2nd degree equation Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while Possible solution 2nd degree equation Solve equation? One equation * While answer  n or N Information 1 Constant a 3 Constant c 5 Constant b 4 If D  0 SolutionNo real solution 11 2 2 One solution If D  0 7 8 6 9 10 Two solutions

77 Lego Mindstorms

78

79 Here’s an example of translating the pseudocode to C code Solving equations C Program

80 Solving equations C coding Solving 2nd degree equations int main() { char answer; I’m lost in cyberspace

81 Solving equations C coding Solving 2nd degree equations int main() { char answer; I’m lost in cyberspace

82 Solving equations C coding Solving 2nd degree equations int main() { char answer; I’m lost in cyberspace

83 Solving equations C coding Solving 2nd degree equations int main() { char answer;

84 Solving equations C coding Solving 2nd degree equations Display program information int main() { char answer; info(); Don’t worry! You’ll C!! void info(void) { printf(”\nThis program will solve equations of the 2nd degree”); printf(”\n in the form of ax^2+bx+c=0.”); printf(”\nReturns only real values.”); printf(”\n”); }

85 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) int main() { char answer; info(); answer=ask_again(); Aha, this doesn´t seem to be that difficult! char ask_again(void) { char c; printf(”\n Do you want to solve an equation? (y/n)”); while((c=getchar())==’\n’); return c; }

86 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) int main() { char answer; info(); answer=ask_again(); Make room! I’ve got more to say! char ask_again(void) { char c; printf(”\n Do you want to solve an equation? (y/n)”); while((c=getchar())==’\n’); return c; }

87 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) int main() { char answer; info(); answer=ask_again(); Make room! I’ve got more to say! char ask_again(void) { char c; printf(”\n Do you want to solve an equation? (y/n)”); while((c=getchar())==’\n’); return c; }

88 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { Make room! I’ve got more to say!

89 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { Make room! I’ve got more to say!

90 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { Make room! I’ve got more to say!

91 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, a, b, c);

92 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a;

93 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 else end if else end if int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) { } else { } } else { } If and else? I don’t get it!

94 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 else end if else end if int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) { } else { } } else { } If and else? I don’t get it! Wait, check this out!

95 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 else end if else end if int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) { } else { } } else { } Voila! calculate x = -b/(2a) display x x = -b/(2*a); printf(”One solution X:%.2lf \n”, x);

96 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else end if else end if int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) { x = -b/(2*a); printf(”One solution X:%.2lf\n”, x); } else { } } else { } calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 x1 = -b/(2*a)+ sqrt(D); x2 = -b/(2*a)- sqrt(D); printf(”Two solutions X1:%.2lf X2:%.2lf\n”, x1, x2);

97 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else end if int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) { x = -b/(2*a); printf(”One solution X:%.2lf\n”, x); } else { x1 = -b/(2*a)+ sqrt(D); x2 = -b/(2*a)- sqrt(D); printf(”Two solutions X1:%.2lf X2:%.2lf\n”, x1, x2); } } else { } display No solution printf(”No solutions!\n”);

98 Solving equations C coding Solving 2nd degree equations Display program information Ask if user would like to solve an equation (y/n) While answer is not n or N Input constant a Input constant b Input constant c calculate D = b²/(4a²)-c/a If D  0 If D = 0 calculate x = -b/(2a) display x else calculate x 1 = -b/(2a)+  D calculate x 2 = -b/(2a)-  D display x 1, x 2 end if else display No solution end if Ask if user would like to solve an equation (y/n) end while int main() { char answer; info(); answer=ask_again(); while(answer != ’n’ && answer != ’N’) { printf(”Input a b c with spaces:”); scanf(”%lf%lf%lf”, &a, &b, &c); D = pow(b, 2)/(4*pow(a,2))-c/a; if (D >= 0) { if (D == 0) { x = -b/(2*a); printf(”One solution X:%.2lf\n”, x); } else { x1 = -b/(2*a)+ sqrt(D); x2 = -b/(2*a)- sqrt(D); printf(”Two solutions X1:%.2lf X2:%.2lf\n”, x1, x2); } } else { printf(”No solutions!\n”); } answer=ask_again(); } Program

99 THE END


Download ppt "Structured thinking Part 1: 1. Introduction 2. Structure Diagram 3. Comparison Part 2: 4. Examples 5. Rules 7. More examples Part 3: 8. Structure - pseudocode."

Similar presentations


Ads by Google