faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/csoverview/flowcharting algorith… · web...
TRANSCRIPT
Flowcharting AlgorithmsRepresenting Algorithms
Natural languageo Language spoken and written in everyday lifeo Examples: English, Spanish, Arabic, and so ono Problems with using natural language for algorithms
Verbose Imprecise Relies on context and experiences to give precise meaning to a
word or phrase
The Addition Algorithm Expressed in Natural Language
High-level programming languageo Examples: C++, Javao Problem with using a high-level programming language for algorithms
During the initial phases of design, we are forced to deal with detailed language issues
Expressed in a High-Level Programming Language
Pseudocode English language constructs modeled to look like statements available in most
programming languages Steps presented in a structured manner (numbered, indented, and so on) No fixed syntax for most operations is required Less ambiguous and more readable than natural language Emphasis is on process, not notation Well-understood forms allow logical reasoning about algorithm behavior Can be easily translated into a programming language
Psuedocode in Sequential Operations Types of algorithmic operations
o Sequentialo Conditionalo Iterative
Computation operationso Ex: Set the value of “variable” to “arithmetic expression”
Variableo Named storage location that can hold a data value
Input operationso To receive data values from the outside worldo Ex: Get a value for r, the radius of the circle
Output operations
o To send results to the outside world for displayo Ex: Print the value of Area
Algorithm for Computing Average Miles per Gallon
Lupoli’s Laws of Psuedocode1. Layout general algorithm first2. Determine
1. Number of variables needed2. Type of values required (received from user)3. Items to display for directions4. Items to display for results
3. Questions that were not answered in the original problem1. Ex: What types of data are we receiving??
Create an algorithm to find the standard deviation of five numbers (in a program) Team breakdown
1. Scriber (one piece of paper) psuedocode2. Claim your land (on boards) write all members name on a spot on the
board3. Coder AFTER conversing, write code on board
YOU CAN USE GOOGLE TO DETMERINE WHAT DEVIATION DOESAnswerb
Conditional Operations Sequential algorithm
o Also called straight-line algorithmo Executes its instructions in a straight line from top to bottom and then
stops Control operations
o Conditional operationso Iterative operations
Conditional operationso Ask questions and choose alternative actions based on the answers
Conditional operations Example
if x is greater than 25 thenprint x
elseprint x times 100
1. Using the example above, create a Psuedoecode example for passing or failing a class (passing grade can be whatever YOU decide) Write on paper.2. Using the example above, create a Psuedocode example for grades A-F(E). Write this one on the board!!
Second Version of the Average Miles per Gallon Algorithm
Iterative Operations Iterative operations
o Perform “looping” behavior, repeating actions until a condition becomes false
o Loop The repetition of a block of instructions
Components of a loopo condition
loop ends when the condition is false o Loop body
Infinite loopo The condition never becomes falseo An error
Iterative Exampleswhile j > 0 do
set s to s + ajset j to j – 1
How many times will this loop run (if j = 100)?What will j be when the loop is done??What happens to j each time through the loop?
repeatprint akset k to k + 1
until k > n
What is the difference between the two examples above?
Third Version of the Average Miles per Gallon Algorithm
Pretest loop (“Ask first, shoot later”)o Condition tested at the beginning of each pass through the loopo It is possible for the loop body to never be executedo While loop
Posttest loop (“shoot first, ask later”)o Condition tested at the end of loop bodyo Loop body must be executed at least onceo Do/While loop
Summary of Pseudocode Language Instructions
What is a Flowchart? diagram that uses graphic symbols to depict the nature and flow of the steps in
a process.
When should teams use Flowcharts? beginning
o process to understand how it currently works. Later
o team will develop a Flowchart of the modified process—again, to record how it actually functions.
Finallyo team may want to create an ideal Flowchart to show how you would
ultimately like the process to be performed.
Benefits of using Flowcharts Promote understanding of a process by explaining the steps pictorially. Provide a tool for training employees. Identify problem areas and opportunities for process improvement. Depict customer-supplier relationships
Levels of Flowchart detail When you are developing a Flowchart, consider how it will be used and the
amount and kind of information needed by the people who will use it Levels
Levels of Flowcharts
Macro level.
the "big picture" view of the process. Generally, a macro-level Flowchart has fewer than six steps.
Mini level.
used for a Flowchart that falls between the big picture of the micro level and the fine detail of the micro level.
Micro level
provides a very detailed picture of a specific portion of the process by documenting every action and decision.
Flowcharting SymbolsSymbol Name Meaning
Flow line used to connect symbols and indicate the flow of logic
Terminal used to represent the beginning (START) or end (END) of a task
Input/Output used for input and output operations, such as reading in from the keyboard (user input) or outputting to the screen
Processing used for arithmetic, code, and data manipulations. Multiple lines can be placed in one of these.
Decision used for ANY logical comparison like if/else, <, >, &&, ||, !, etc…
Connector if you have multiple pages, place a number inside this symbol so it can match another with the same number
Predefined Process/Method
used to represent a method call, or variables are sent to another method to return data or an answer
Note/Annotation used to add personal notes to your project
ALL IN MICROSOFT WORD!!!o Drawing Autoshapes Flowchart
How do we get started? Methods for constructing Flowcharts:1. Identify the right people to develop the chart.2. Determine what you expect to get from the Flowchart.3. Identify who will use it and how.4. Define the level of detail you need.5. Establish the boundaries of the process to be improved.
Create a Flowchart the given “honey do” list1. Spouse says “Cut the grass.”2. Check oil and fill gas3. Cut the grass4. Does lawnmower need to be washed?5. Get hose and wash mower
(branches from decision merge here)
6. Return mower to garage7. Open garage door, insert mower8. Pull weed eater to driveway9. Check gas and oil10. Need gas or oil?11. Get gas & oil from garage.
(branches from decision merge here)
12. Start weed eater13. Weed eat the yard14. Turn weed eater off15. Put weed eater away16. Close garage
Example If-decisions flowchart setups
YES NO
NO YES NO
YES NO
YES NO
YES NO
Drawing Flowcharts As much as possible draw from the top of the page towards the bottom
o There will be times that it will be difficult, but remember to make an effort
ExercisesYou tell ME what is on your autonomous robot!!! (No WEAPONS or TELEPORTATION or FLYING!! It must use current technology.) The room will not be damaged. The robot is at the printer facing out (toward isle or door). Your TA will test your psuedocode!
1. Teammate #1, Create the psuedocode for a robot to leave the classroom and make it’s way OUTSIDE (like outdoors). Complete in a Word document, then print.
2. Teammate #2, using #1 flowchart for a robot to leave the classroom and make it’s way OUTSIDE (like outdoors). Complete in a Word document, then print!! (Note, this will take about an hour)
E-mail to
C
B
A
Pass!!!
Fail!!!
if
C
So how can we make our Pseudocode/Flowcharts better?1. Better organization(indenting)2. Simulating in the Environment3. Better communication between user and programmer4. Simple can be good!
Answer Section
Psuedocode for averaging numbers
1. Layout general algorithm first
Printing you nameDeclaring variablesAsking for the valuesReceiving the valuesCalculate the averageDescribing the results
2. Determinea. Number of variables needed (4 – num1, num2, num3, and average)b. Type of values required (received from user) (all float)
3. Questions that were not answered in the original problem
What types of data are we receiving??Redisplay the numbers they entered??