24 CHAPTER 3.ALGORITHMS AND PSEUDOCODES 3.2 Flowchart representation START STEP1 STEP 2 STEP 3 STOP The above figure is a representation of a simple sequence of 3 steps with a beginning and an end.The START and STOP are represented by circles.A sequence step is represented by a rectangle.The logic flow is indicated by lines and arrows.This kind of representation is called a flowchart or schematic diagram.It looks a lot like a circuit diagram in electronics. 3.3 Pseudocode representation An algorithm is a well-defined description of actions and their ordering.Pseudocode is an- other way of expressing the actions and ordering to be taken place on a computer in an exact way.Yet,pseudocode does it in an informal way,without getting bogged-down in the details or syntax of a particular computer language like C++,MATLAB or something else. Two programmers should be able to agree on pseudocode and write functionally equivalent computer programs-either in the same computer language or in different ones.Pseudocode can be written in any human language in the form of an ordered (or numbered)list.For example,a pseudocode representation of the above flowchart would be: START→Step1→Step2→Step3→STOP A more conventional representation has the instruction flow (also called logic flow)going from top to bottom:
24 CHAPTER 3. ALGORITHMS AND PSEUDOCODES 3.2 Flowchart representation STEP 1 STEP 2 STEP 3 START STOP The above figure is a representation of a simple sequence of 3 steps with a beginning andan end. The START and STOP are representedby circles. A sequence step is representedby a rectangle. The logic flow is indicatedby lines andarrows. This kindof representation is calleda flowchart or schematic diagram. It looks a lot like a circuit diagram in electronics. 3.3 Pseudocode representation An algorithm is a well-defined description of actions and their ordering. Pseudocode is another way of expressing the actions andordering to be taken place on a computer in an exact way. Yet, pseudocode does it in an informal way, without getting bogged-down in the details or syntax of a particular computer language like C++, MATLAB or something else. Two programmers should be able to agree on pseudocode and write functionally equivalent computer programs—either in the same computer language or in different ones. Pseudocode can be written in any human language in the form of an ordered (or numbered) list. For example, a pseudocode representation of the above flowchart would be: START =⇒ Step 1 =⇒ Step 2 =⇒ Step 3 =⇒ STOP A more conventional representation has the instruction flow (also calledlogic flow) going from top to bottom:
3.4.DECISIONS,CONDITIONALS,BRANCH INSTRUCTIONS 25 START Step 1 Step 2 Step 3 STOP 3.4 Decisions,conditionals,branch instructions A "conditional"instruction or statement,also called a decision instruction,a branch or de- cision statement,is one in which a decision is made and the logic flow can follow different paths,depending on the answer.In pseudocode it can be represented as follows: IF such-and-such THEN DO this-and-that OTHERWISE DO something-else One could do the same thing with a flowchart,and it would look like: YES SUCH NO & SUCH THIS SOMETHING & THAT ELSE Note that we have introduced a new shape to handle the case where,depending on the input, a decision is made and the logic flow can follow two different paths.This is represented by a diamond shape".A branch statement or a decision statement is characterized by only one path in and always two paths out. 3.5 Looping or jump instructions A final ingredient is needed before we complete our abstract discussion of algorithms-the ability for a statement to transfer control not to the next statement,but somewhere else in
3.4. DECISIONS, CONDITIONALS, BRANCH INSTRUCTIONS 25 START Step 1 Step 2 Step 3 STOP 3.4 Decisions, conditionals, branch instructions A “conditional” instruction or statement, also called a decision instruction, a branch or decision statement, is one in which a decision is made and the logic flow can follow different paths, depending on the answer. In pseudocode it can be represented as follows: IF such-and-such THEN DO this-and-that OTHERWISE DO something-else One coulddo the same thing with a flowchart, andit wouldlook like: YES NO SUCH & SUCH THIS & THAT SOMETHING ELSE Note that we have introduced a new shape to handle the case where, depending on the input, a decision is made and the logic flow can follow two different paths. This is represented by a “diamond shape”. A branch statement or a decision statement is characterizedby only one path in andalways two paths out. 3.5 Looping or jump instructions A final ingredient is needed before we complete our abstract discussion of algorithms —the ability for a statement to transfer control not to the next statement, but somewhere else in
26 CHAPTER 3.ALGORITHMS AND PSEUDOCODES the algorithm.This is called a "looping”or“jump”instruction. Consider the following pseudocode: ITEM 3 a set of other steps JUMP TO:ITEM 3 The stuff between ITEM 3 and JUMP TO:ITEM 3 inclusive,defines a loop.This is represented in a flowchart as follows with the direction of logic flow made clear with the lines and arrows. ITEM 3 JUMP TO ITEM 3 3.6 Sequencing,branching and looping It can be shown that the 3 constructs of sequencing,branching and looping,are all that one needs to construct any computer algorithm!Thus,the "theory"of algorithmic flow boils down to understanding just these three things.There are many,many ways of expressing these three things in a computer language like C++or Matlab.But remember that it all boils down to just sequencing,branching and looping.These are the three pillars of algorithmic design! Of course there is a lot of other stuff to be considered to make a computer language useful. We'll see a lot of this in the course.However,as far as the flow of logic goes,we have done it all at this point!You can now go and write an algorithm to do anything you want and express it in either pseudocode or flowchart form
26 CHAPTER 3. ALGORITHMS AND PSEUDOCODES the algorithm. This is calleda “looping” or “jump” instruction. Consider the following pseudocode: ITEM 3 . . a set of other steps . . JUMP TO: ITEM 3 The stuff between ITEM 3 and JUMP TO: ITEM 3 inclusive, defines a loop. This is represented in a flowchart as follows with the direction of logic flow made clear with the lines andarrows. ITEM 3 JUMP TO ITEM 3 3.6 Sequencing, branching and looping It can be shown that the 3 constructs of sequencing, branching andlooping, are all that one needs to construct any computer algorithm! Thus, the “theory” of algorithmic flow boils down to understanding just these three things. There are many, many ways of expressing these three things in a computer language like C++ or Matlab. But remember that it all boils down to just sequencing, branching and looping. These are the three pillars of algorithmic design! Of course there is a lot of other stuff to be considered to make a computer language useful. We’ll see a lot of this in the course. However, as far as the flow of logic goes, we have done it all at this point! You can now go andwrite an algorithm to do anything you want and express it in either pseudocode or flowchart form
3.7.A MINI-SUMMARY BEFORE THE EXAMPLES 27 3.7 A mini-summary before the examples PSEUDOCODE:The language and organization of pseudocode is not firmly established. Make any convention for yourself that makes your algorithm clear.Note the use of spacing and indenting,bold and italic fonts,all ways of emphasizing meaning. FLOWCHARTS:It is also possible to write pseudocode diagrammatically in terms of flow charts that represent the flow of computer instructions,or "logic flow".Here there are some accepted (but not rigorously adopted)standards.These charts look very much like circuit diagrams.It is considered good practice to describe every variable you use and provide a well-defined starting point and ending point. Pseudocode and flow charts are indispensable aids in getting a computer program "up and running"with a minimum of effort. 3.8 Some examples
3.7. A MINI-SUMMARY BEFORE THE EXAMPLES 27 3.7 A mini-summary before the examples PSEUDOCODE: The language and organization of pseudocode is not firmly established. Make any convention for yourself that makes your algorithm clear. Note the use of spacing andindenting, boldanditalic fonts, all ways of emphasizing meaning. FLOWCHARTS: It is also possible to write pseudocode diagrammatically in terms of flow charts that represent the flow of computer instructions, or “logic flow”. Here there are some accepted (but not rigorously adopted) standards. These charts look very much like circuit diagrams. It is considered good practice to describe every variable you use and provide a well-defined starting point and ending point. Pseudocode and flow charts are indispensable aids in getting a computer program “up and running” with a minimum of effort. 3.8 Some examples
28 CHAPTER 3.ALGORITHMS AND PSEUDOCODES 3.8.1 The Breakfast algorithm,or,Bielajew's Sunday Morning Internationally-famous pancakes 1.Start of The Breakfast Algorithm 2.If the day is not Sunday,have your usual boring bowl of Granola.For added excitement, slice in half a banana.Otherwise, (a)In a big bowl,whisk together 1 and 1/2 cups white flour and 2 heaping teaspoons of baking powder. (b)In a separate bowl,beat two whole eggs (no shells please)together for one minute or so. (c)Add 1 cup of milk,preferably skimmed,to the eggs and mix. (d)Add 1 tablespoon of Canola oil,3 tablespoons of maple syrup,and one teaspoon of vanilla to the milk and eggs and mix. (e)Add the wet stuff to the dry stuff. (f)Take a big spoon and, (g)Mix the stuff in the big bowl one whole turn. (h)If there are lots of lumps in the mixture,go back to step 2.(g),otherwise go to step 2.(i).Technical note:Don't go overboard and make it completely smooth. Leave it a little lumpy for terture. (i)Slice in a Granny Smith apple (no peel)and (optionally)a half cup of frozen cranberries (well rinsed). (j)Add a little bit of Canola oil to an electric fry pan preheated to 350F.Put in just enough to coat the bottom of the pan. (k)Put the pancake dough into the pan,making approximately four 3-4"diameter circles.Turn the heat down to 300F.Fry for exactly 3 minutes with the pan covered,flip and fry for 3 more minutes with the pan uncovered. (1)Take them out of the frying pan and place on a napkin to absorb excess oil. (m)Eat with maple syrup and sliced strawberries on top.I DARE you to eat more than 3!(It has never been done...) 3.Clean up your dishes. 4.End of The Breakfast Algorithm This algorithm produces about 8 pancakes,enough for 4-6 people
28 CHAPTER 3. ALGORITHMS AND PSEUDOCODES 3.8.1 The Breakfast algorithm, or, Bielajew’s Sunday Morning Internationally-famous pancakes 1. Start of The Breakfast Algorithm 2. If the day is not Sunday, have your usual boring bowl of Granola. For added excitement, slice in half a banana. Otherwise, (a) In a big bowl, whisk together 1 and1/2 cups white flour and2 heaping teaspoons of baking powder. (b) In a separate bowl, beat two whole eggs (no shells please) together for one minute or so. (c) Add 1 cup of milk, preferably skimmed, to the eggs and mix. (d) Add 1 tablespoon of Canola oil, 3 tablespoons of maple syrup, and one teaspoon of vanilla to the milk andeggs andmix. (e) Add the wet stuff to the dry stuff. (f) Take a big spoon and, (g) Mix the stuff in the big bowl one whole turn. (h) If there are lots of lumps in the mixture , go back to step 2.(g), otherwise go to step 2.(i). Technical note: Don’t go overboard and make it completely smooth. Leave it a little lumpy for texture. (i) Slice in a Granny Smith apple (no peel) and(optionally) a half cup of frozen cranberries (well rinsed). (j) Adda little bit of Canola oil to an electric fry pan preheatedto 350◦F. Put in just enough to coat the bottom of the pan. (k) Put the pancake dough into the pan, making approximately four 3–4” diameter circles. Turn the heat down to 300◦F. Fry for exactly 3 minutes with the pan covered, flip andfry for 3 more minutes with the pan uncovered. (l) Take them out of the frying pan andplace on a napkin to absorb excess oil. (m) Eat with maple syrup andslicedstrawberries on top. I DARE you to eat more than 3! (It has never been done...) 3. Clean up your d ishes. 4. Endof The Breakfast Algorithm This algorithm produces about 8 pancakes, enough for 4–6 people