Lecture 1.2_ Flowcharts.pdf

Full Transcript

Lecture 1.2 Flowcharts Lecture Overview 1. Introduction to Flowcharts 2. Example Flowchart: Guessing Game 3. Designing Effective Flowcharts 2 1. Introduction to Flowcharts 3 Motivation ◎ Let's say you want to start a housecleaning business. ◎ You hire three employees and start taking custom...

Lecture 1.2 Flowcharts Lecture Overview 1. Introduction to Flowcharts 2. Example Flowchart: Guessing Game 3. Designing Effective Flowcharts 2 1. Introduction to Flowcharts 3 Motivation ◎ Let's say you want to start a housecleaning business. ◎ You hire three employees and start taking customers. ◎ After running your business for a while, you start receiving complaints about inconsistent results. ◎ You decide to investigate: ○ You take each employee to a bedroom, and instruct them to “clean the room”. ○ You observe different processes! 4 What Went Wrong? ◎ “Clean the room” is not a precise description of the task to be performed. ◎ Human beings fill in the gaps based on “human intuition”: ○ Past experiences, ○ Preferences, ○ Mood, ○ etc. 5 What Can Be Done? ◎ Document the steps in detail, without room for interpretation. ○ i.e. design the algorithm. ◎ Hand out the detailed instructions. ◎ Each employee can now follow the exact same process. 6 Motivation ◎ Describing processes in detail is especially important for machines. ◎ Computers typically do exactly what they are told and lack any “human intuition”. ○ Recall the baby robot analogy. ◎ This means that a detailed algorithm is not just preferable---it is required. 7 Describing Processes ◎ One way of describing a process is through written paragraphs. ◎ Complicated processes can be difficult to read. Begin by dusting the furniture and blinds. Next, vacuum the floor. If the floor is carpeted, then you should shampoo the carpet. Otherwise, if the floor is not carpeted, mop the floor. 8 Describing Processes ◎ Another way of describing a process is with a flowchart. ◎ A flowchart provides a clear visual representation. 9 What is a Flowchart? ◎ A flowchart diagram details the flow of a process. ○ Used to document or communicate a process. ○ Describes steps clearly and unambiguously. ○ Applicable to both physical processes and computer processes. 10 What is a Flowchart? ◎ Flowcharts can be a useful tool for designing computer programs before writing code. ○ Helps you to think through the individual decisions and steps involved in a process. ○ Can be translated into actual programming code afterwards. 11 The Origin of Flowcharts ◎ The first flowcharts were introduced in 1921. ◎ Covered workflows like “loading rifle grenades”. 12 Elements of a Flowchart ◎ A flowchart consists of shapes joined with arrows. ○ The arrows describe the flow of the process. ○ The shapes describe steps in the process. ◎ We will now describe the meaning of each of the different shapes. 13 Start/Stop Elements ◎ Start/stop elements describe points where a process begins and ends. ◎ Start element: 1 outgoing arrow. Start ◎ Stop element: 1 incoming arrow. ◎ Each flowchart should include one start element and one stop element. ◎ Represented rectangles. using Stop rounded 14 Process Elements ◎ Process elements describe a processing step. ◎ 1 incoming arrow and 1 outgoing arrow. ◎ Description usually starts with a verb (action). ◎ Flowcharts can include many process elements. Pay the bill ◎ Represented using rectangles. 15 Input/Output Elements ◎ Input/output elements describe points where data enters/leaves a program. ○ e.g. Accepting user input, displaying results. Input name ◎ 1 incoming arrow and 1 outgoing arrow. ◎ Flowcharts typically include at least one input and one output element. Display cost ◎ Represented using parallelograms. 16 Decision Elements ◎ Decision elements select between multiple flows based on some kind of test condition. ◎ 1 incoming arrow and multiple outgoing arrows. no Is age ≥ 65? ◎ Often phrased as a yes/no question. ◎ Represented using diamonds. yes 17 Connectors ◎ Connector elements join multiple flows. ◎ Multiple incoming arrows and 1 outgoing arrow. ◎ Useful for rejoining multiple flows after a decision. ◎ Represented using circles. 18 Check Your Understanding Q. What are the expected outputs of the program described by this flowchart when the user inputs an age of 19? 19 Check Your Understanding Q. What are the expected outputs of the program described by this flowchart when the user inputs an age of 19? A. “adult” and “ticket”. “adult” is output because age (19) is not less than 18. “ticket” is always output irrespective of the input age. 20 2. Example Flowchart: Guessing Game 21 Task Definition The computer picks a number between 1 and 100, and the user tries to guess that number. Whenever the user makes an incorrect guess, they are told whether the number is higher or lower than their guess. When the user correctly guesses the number, a victory message is displayed and the program stops. 22 Example Behaviour 1. 2. 3. 4. 5. 6. 7. 8. 9. Generate a random number (x ← 44). Input user guess (y ← 25). Is y equal to x? No. Is x greater than y? Yes, display “Higher”. Input user guess (y ← 50). Is y equal to x? No. Is x greater than y? No, display “Lower”. Input user guess (y ← 44). Is y equal to x? Yes, display “Correct”. 23 Identifying Flowchart Elements ◎ We can analyse the example behaviour to identify flowchart elements: ○ Input/output ○ Decision ○ Process 24 Identifying Input/Output Elements 1. 2. 3. 4. 5. 6. 7. 8. 9. Generate a random number (x ← 44). Input user guess (y ← 25). Is y equal to x? No. Is x greater than y? Yes, display “Higher”. Input user guess (y ← 50). Is y equal to x? No. Is x greater than y? No, display “Lower”. Input user guess (y ← 44). Is y equal to x? Yes, display “Correct”. 25 Identifying Input/Output Elements ◎ Inputs: ○ User guess (a number, y). ◎ Possible outputs: ○ Higher hint (“Higher”). ○ Lower hint (“Lower”). ○ Victory message (“Correct”). Input guess as y Display “Higher” Display “Lower” Display “Correct” 26 Identifying Decision Elements 1. 2. 3. 4. 5. 6. 7. 8. 9. Generate a random number (x ← 44). Input user guess (y ← 25). Is y equal to x? No. Is x greater than y? Yes, display “Higher”. Input user guess (y ← 50). Is y equal to x? No. Is x greater than y? No, display “Lower”. Input user guess (y ← 44). Is y equal to x? Yes, display “Correct”. 27 Identifying Decision Elements ◎ Is y equal to x? ◎ Is x greater than y? Is y == x? Is x > y? 28 Identifying Processing Elements 1. 2. 3. 4. 5. 6. 7. 8. 9. Generate a random number (x ← 44). Input user guess (y ← 25). Is y equal to x? No. Is x greater than y? Yes, display “Higher”. Input user guess (y ← 50). Is y equal to x? No. Is x greater than y? No, display “Lower”. Input user guess (y ← 44). Is y equal to x? Yes, display “Correct”. 29 Identifying Processing Elements ◎ Generate a random number (x). Generate a random number between 1 and 100 as x 30 Constructing the Flowchart ◎ Now that we have the elements, we can construct the flowchart. ◎ Need to think about the flow (order of steps). ◎ Once again, the example behaviour is a good reference. 31 Start Generate a random number between 1 and 100 as x Input guess as y Is y == x? yes no Is x > y? yes Display “Higher” no Display “Lower” Display “Correct” Stop 32 Next Steps ◎ Computers can’t read flowcharts like this one directly. ◎ The next step towards creating a working program would be to translate the flowchart into code (program). ○ This will be covered in future lectures. ◎ Having a well-designed flowchart makes writing code much easier. 33 3. Designing Effective Flowcharts 34 Order is Important ◎ Order is important. ◎ Think carefully about the order in which things should happen as you draw the flowchart. Would you trust this recipe? 1. Take the cake out of the oven. 2. Mix ingredients thoroughly. 3. Allow the cake to cool. 4. Put the cake into the oven. 5. Add ingredients into the bowl. 35 Clear, Concise, and Complete ◎ Clear ○ Space elements out. ○ Avoid crossing over arrows where possible. ◎ Concise ○ Use short, accurate descriptions. ○ Avoid unnecessary duplication. ◎ Complete ○ Cover all possible eventualities. 36 Start Generate a random number between 1 and 100 as x ✗ Incomplete. What happens if x is not greater than y? Input guess as y Is y == x? yes no Is x > y? yes Display “Higher” Display “Correct” Stop 37 Start Generate a random number between 1 and 100 as x ✗ Unnecessarily repeated elements add clutter. Input guess as y Is y == x? Input guess as y yes Input guess as y no Is x > y? yes Display “Higher” no Display “Lower” Display “Correct” Stop 38 Top-To-Bottom Layout ◎ Aim for the process to flow from top to bottom. ◎ Arrows should generally point downwards. ◎ Put the start element at the top of the page. ◎ Put the stop element at the bottom of the page. ◎ Sometimes decisions cause the flow to go back up, this is OK. 39 Input guess as y ✗ Inconsistent flow direction is hard to follow. Stop no Is y == x? yes Display “Correct” Display “Higher” yes Generate a random number between 1 and 100 as x Is x > y? no Start Display “Lower” 40 Know Your Arrows ◎ Different elements have different numbers of incoming and outgoing arrows. ◎ Check that each element in your flowchart has the correct number of arrows. ◎ Use connectors where appropriate. 41 ELEMENT INCOMING ARROWS OUTGOING ARROWS Start 0 1 Stop 1 0 Process 1 1 Input/Output 1 1 Decision 1 2+ 2+ 1 Connector A table showing the correct number of arrows for each flowchart element. 42 Start ✗ Generate a random number between 1 and 100 as x Input guess as y Is y == x? no Is x > y? Incorrect number of incoming arrows. Use connectors to make joined flows obvious. yes yes Display “Higher” no Display “Lower” Display “Correct” Stop 43 Flowchart Creation Checklist ✓ Is there one start element and one stop element? ✓ Are the elements in a logical order? ✓ Is the flowchart clear, concise, and complete? ✓ Does the flowchart flow from top to bottom? ✓ Is the number of arrows correct? 44 Summary 45 In This Lecture We... ◎ Discovered the purpose of flowcharts. ◎ Learnt the essential flowchart elements. ◎ Worked through creating a flowchart from a task definition. ◎ Explored best flowcharts. practices for designing effective 46 Next Lecture We Will... ◎ Begin learning basic elements of programming code: ○ Variables, ○ Statements, and ○ Comments. 47 Labs Start Next Week (Week 2) ◎ Please attend the lab class that you're enrolled in. ◎ Bring your laptop if you have one. ○ You will be setting up programming software. 48 Thanks for your attention! The slides and lecture recording will be made available on LMS. The “Cordelia” presentation template by Jimena Catalina is licensed under CC BY 4.0. PPT Acknowledgement: Dr Aiden Nibali, CS&IT LTU. 49

Use Quizgecko on...
Browser
Browser