{"id":35,"date":"2019-01-15T23:22:58","date_gmt":"2019-01-15T23:22:58","guid":{"rendered":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/chapter\/how-to-think-like-an-engineer\/"},"modified":"2019-01-28T19:07:03","modified_gmt":"2019-01-28T19:07:03","slug":"how-to-think-like-an-engineer","status":"publish","type":"chapter","link":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/chapter\/how-to-think-like-an-engineer\/","title":{"raw":"UNIT 1: How to Think Like an Engineer","rendered":"UNIT 1: How to Think Like an Engineer"},"content":{"raw":"<h1>Learning Objectives<\/h1>\r\n<ul>\r\n \t<li>Explain what we mean by \u201cComputational Thinking\u201d.<\/li>\r\n \t<li>Describe the problem being solved in a computational algorithm.<\/li>\r\n \t<li>Explain the process for generating computational algorithms.<\/li>\r\n \t<li>Generate and test algorithms to solve computational problems.<\/li>\r\n \t<li>Evaluate computational algorithms for exactness, correctness, termination, generalizability and understandability.<\/li>\r\n \t<li>Explain the role of programming in the field of Informatics.<\/li>\r\n<\/ul>\r\n<h1>Introduction<\/h1>\r\n<p class=\"import-Normal\">The goal of this book is to teach you to solve computational problems and to think like an engineer. Computational problems are problems that can be solved by the use of computations (a computation is what you do when you calculate something). Engineers are people who solve problems - they invent, design, analyze, build and test \"things\" to fulfill objectives and requirements. The single most important skill for you to learn is problem solving. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of learning to program is an excellent opportunity to practice problem-solving skills.<\/p>\r\n<p class=\"import-Normal\">This book strives to prepare you to write well-designed computer programs that solve interesting problems involving data.<\/p>\r\n\r\n<h1>Computational Thinking<\/h1>\r\n[caption id=\"\" align=\"alignright\" width=\"460\"]<img src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232232\/image3.jpg\" alt=\"computational thinking chart\" width=\"460\" height=\"306\" \/> Figure 1:\u00a0\"The seven components to computational thinking\"(www.ignitemyfutureinschool.org\/about)[\/caption]\r\n<p class=\"import-Normal\"><em>Computational Thinking<\/em> is the thought processes involved in understanding a problem and expressing its solution in a way that a computer can effectively carry out. Computational thinking involves solving problems, designing systems, and understanding human behavior (e.g. what the user needs or wants) \u2013 thinking like an engineer. Computational thinking is a fundamental skill for everyone, not just for programmers because computational thinking is what comes <em>before<\/em> any computing technology.[footnote]Wing, Jeannette M. \"Computational thinking.\" Communications of the ACM 49.3 (2006): 33-35.[\/footnote]<small><\/small><\/p>\r\n<p class=\"import-Normal\">Computer science is the study of computation \u2014 what can be computed and how to compute it whereas computational thinking is:<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>Conceptualizing<\/strong>, not programming. Computer science is not only computer programming. Thinking like a computer scientist means more than being able to program a computer. It requires thinking at multiple levels of abstraction;<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>Fundamental<\/strong>, not rote skill. A fundamental skill is something every human being must know to function in modern society. Rote means a mechanical routine;<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>A way that humans, not computers, think<\/strong>. Computational thinking is a way humans solve problems; it is not trying to get humans to think like computers. Computers are dull and boring; humans are clever and imaginative. We humans make computers exciting. Equipped with computing devices, we use our cleverness to tackle problems we would not dare take on before the age of computing and build systems with functionality limited only by our imaginations;<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>Complements and combines mathematical and engineering thinking<\/strong>. Computer science inherently draws on mathematical thinking, given that, like all sciences, its formal foundations rest on mathematics. Computer science inherently draws on engineering thinking, given that we build systems that interact with the real world;<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>Ideas<\/strong>, not artifacts. It\u2019s not just the software and hardware artifacts we produce that will be physically present everywhere and touch our lives all the time, it will be the computational concepts we use to approach and solve problems, manage our daily lives, and communicate and interact with other people;<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>For everyone, everywhere<\/strong>. Computational thinking will be a reality when it is so integral to human endeavors it disappears as an explicit philosophy.[footnote]Wing, Jeannette M. \"Computational thinking.\" Communications of the ACM 49.3 (2006): 33-35.[\/footnote]<\/p>\r\n\r\n<h1>Algorithms<\/h1>\r\n[caption id=\"attachment_278\" align=\"alignright\" width=\"369\"]<img class=\"wp-image-278\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232235\/Areyouhappy.jpg\" alt=\"\" width=\"369\" height=\"529\" \/> Figure 2 \"Are you happy?\" by Typcut <a href=\"http:\/\/www.typcut.com\/headup\/are-you-happy\">http:\/\/www.typcut.com\/headup\/are-you-happy<\/a>[\/caption]\r\n<p class=\"import-Normal\">An algorithm specifies a series of steps that perform a particular computation or task. Throughout this book we\u2019ll examine a number of different algorithms to solve a variety of computational problems.<\/p>\r\n<p class=\"import-Normal\">Algorithms resemble recipes. Recipes tell you how to accomplish a task by performing a number of steps. For example, to bake a cake the steps are: preheat the oven; mix flour, sugar, and eggs thoroughly; pour into a baking pan; set the timer and bake until done.<\/p>\r\n<p class=\"import-Normal\">However, \u201calgorithm\u201d is a technical term with a more specific meaning than \u201crecipe\u201d, and calling something an algorithm means that the following properties are all true:<\/p>\r\n\r\n<ol>\r\n \t<li>An algorithm is an unambiguous description that makes clear what has to be implemented in order to solve the problem. In a recipe, a step such as \u201cBake until done\u201d is ambiguous because it doesn\u2019t explain what \u201cdone\u201d means. A more explicit description such as \u201cBake until the cheese begins to bubble\u201d is better. In a computational algorithm, a step such as \u201cChoose a large number\u201d is vague: what is large? 1 million, 1 billion, or 100? Does the number have to be different each time, or can the same number be used again?<\/li>\r\n \t<li>An algorithm expects a defined set of inputs. For example, it might require two numbers where both numbers are greater than zero. Or it might require a word,<span style=\"text-align: initial;font-size: 1em\"> or a <\/span>list<span style=\"text-align: initial;font-size: 1em\"> customer names.<\/span><\/li>\r\n \t<li>An algorithm produces a defined set of outputs. It might output the larger of the two numbers, an all-uppercase version of a word, or a sorted version of the list of names.<\/li>\r\n \t<li>An algorithm is guaranteed to terminate and produce a result, always stopping after a finite time. If an algorithm could potentially run forever, it wouldn\u2019t be very useful because you might never get an answer.<\/li>\r\n \t<li>Must be general for any input it is given. Algorithms solve <em style=\"text-align: initial;font-size: 1em\">general<\/em><span style=\"text-align: initial;font-size: 1em\"> problems (determine if a password is valid); they are of little use if they only solve a <\/span><em style=\"text-align: initial;font-size: 1em\">specific<\/em><span style=\"text-align: initial;font-size: 1em\"> problem (determine if \u2018comp15\u2019 is a valid password)<\/span><\/li>\r\n \t<li>It is at the right level of detail\u2026..the person or device executing the instruction know how to accomplish the instruction without any extra information.<\/li>\r\n<\/ol>\r\n<p class=\"import-Normal\">Once we know it\u2019s possible to solve a problem with an algorithm, a natural question is whether the algorithm is the best possible one. Can the problem be solved more quickly or efficiently?<\/p>\r\n<p class=\"import-Normal\">The first thing you need to do before designing an algorithm is to understand completely the problem given. Read the problem\u2019s description carefully, then read it again. Try sketching out by hand some examples of how the problem can be solved. Finally consider any special cases and design your algorithm to address them.<\/p>\r\n<p class=\"import-Normal\"><strong>An algorithm does not solve a problem rather it gives you a series of steps that, if executed correctly, will result in a solution to a problem.<\/strong><\/p>\r\n\r\n<h1>An Example Algorithm<\/h1>\r\n<p class=\"import-Normal\">Let us look at a very simple algorithm called find_max.<\/p>\r\n<p class=\"import-Normal\"><strong>Problem<\/strong>: Given a list of positive numbers, return the largest number on the list.<\/p>\r\n<p class=\"import-Normal\"><strong>Inputs<\/strong>: A list of positive numbers. This list must contain at least one number. (Asking for the largest number in a list of no numbers is not a meaningful question.)<\/p>\r\n<p class=\"import-Normal\"><strong>Outputs<\/strong>: A number, which will be the largest number in the list.<\/p>\r\n<p class=\"import-Normal\"><strong>Algorithm<\/strong>:<\/p>\r\n\r\n<ol>\r\n \t<li>Start<\/li>\r\n \t<li>Accept a list of positive numbers; set to<code> nums_list<\/code><\/li>\r\n \t<li>Set <code>max_number <\/code>to 0.<\/li>\r\n \t<li>For each number in<code> nums_list, <\/code>compare it to max_number.\r\n<ol type=\"a\">\r\n \t<li>If the number is larger,<code> set max_number <\/code>to the larger number.<\/li>\r\n<\/ol>\r\n<\/li>\r\n \t<li><code>max_number <\/code>is now set to the largest number in the list of positive numbers,<code> nums_list.<\/code><\/li>\r\n \t<li>End<\/li>\r\n<\/ol>\r\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\r\n\r\n<ul>\r\n \t<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\r\n \t<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\r\n \t<li><em>Is it guaranteed to terminate?<\/em> Yes. The list nums_list is of finite length, so after looking at every element of the list the algorithm will stop.<\/li>\r\n \t<li><em>Is it general for any input?<\/em> Yes. A list of any set of positive numbers works.<\/li>\r\n \t<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected<\/li>\r\n<\/ul>\r\n[caption id=\"attachment_84\" align=\"aligncenter\" width=\"609\"]<img class=\"wp-image-84\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232237\/Screen-Shot-2018-07-17-at-8.43.09-AM.png\" alt=\"Figure 3: Example Algorithm\" width=\"609\" height=\"235\" \/> Figure 3: Example Algorithm[\/caption]\r\n<p class=\"import-Normal\">How do we know if an algorithm is unambiguous, correct, comes to an end, is general AND is at the right level of detail? We must test the algorithm. <em class=\"import-Emphasis\">Testing<\/em> means verifying that the algorithm does what we expect it to do. In our \u2018bake a cake\u2019 example we know our algorithm is \u2018working\u2019 if, in the end, we get something that looks, smells and tastes like a cake.<\/p>\r\n\r\n<h1>Verifying your Algorithm<\/h1>\r\n[caption id=\"attachment_282\" align=\"alignright\" width=\"405\"]<img class=\"wp-image-282\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232242\/keyboard-597007_1920.jpg\" alt=\"\" width=\"405\" height=\"273\" \/> Figure 3 \"<a href=\"https:\/\/pixabay.com\/en\/keyboard-computer-facebook-blue-597007\/\">Keyboard<\/a>\" by <a href=\"https:\/\/pixabay.com\/en\/users\/geralt-9301\/\">Geralt<\/a> is licensed under <a href=\"https:\/\/creativecommons.org\/licenses\/by\/2.0\/\">CC 2<\/a>[\/caption]\r\n<p class=\"import-Normal\">Your first step should be to carefully read through EACH step of the algorithm to check for ambiguity and if there is any information missing. To ensure that the algorithm is correct, terminates and is general for any input we devise \u2018test cases\u2019 for the algorithm.<\/p>\r\n<p class=\"import-Normal\">A test case is a set of inputs, conditions, and expected results developed for a particular computational problem to be solved. A test case is really just a question that you ask of the algorithm (e.g. if my list is the three numbers 2, 14, and 11 does the algorithm return the number 14?). The point of executing the test is to make sure the algorithm is correct, that it terminates and is general for any input.<\/p>\r\n<p class=\"import-Normal\">Good (effective) test cases:<\/p>\r\n\r\n<ul>\r\n \t<li>are easy to understand and execute<\/li>\r\n \t<li>are created with the user in mind (what input mistakes will be made? what are the preconditions?)<\/li>\r\n \t<li>make no assumptions (you already know what it is supposed to do)<\/li>\r\n \t<li>consider the boundaries for a specified range of values.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Let us look at the example algorithm from the previous section. The input for the algorithm is \u2018a list of positive numbers\u2019. To make it easy to understand and execute keep the test lists short. The preconditions are that the list only contains numbers and these numbers must be positive so include a test with a \u2018non-number\u2019 (i.e. a special character or a letter) and a test with a negative number. The boundaries for the list are zero and the highest positive number so include a test with zero and a large positive number. That is it! Here is an example of three different test cases.<\/p>\r\n\r\n<div style=\"margin: auto\">\r\n<table style=\"height: 244px;width: 661px\">\r\n<tbody>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 137.433px\">\r\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 283.65px\">\r\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 194.05px\">\r\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 39px;width: 137.433px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 39px;width: 283.65px\">\r\n<p class=\"import-Normal\">List: 44, 14, 0, 1521, 89, 477<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 39px;width: 194.05px\">\r\n<p class=\"import-Normal\">1521<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 137.433px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 283.65px\">\r\n<p class=\"import-Normal\">List: 18, 4, 72, *, 31<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 194.05px\">\r\n<p class=\"import-Normal\">Error (or no result)<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 10px\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 10px;width: 137.433px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 10px;width: 283.65px\">\r\n<p class=\"import-Normal\">List: 22, -9, 52<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 10px;width: 194.05px\">\r\n<p class=\"import-Normal\">Error (or no result)<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<p class=\"import-Normal\">Manually, you should step through your algorithm using each of the three test cases, making sure that the algorithm does indeed terminate and that you get your expected result. As our algorithms and programs become more complex, skilled programmers often break each test case into individual steps of the algorithm\/program and indicate what the expected result of each step should be. When you write a detailed test case, you don\u2019t necessarily need to specify the expected result for each test step if the result is obvious.<\/p>\r\n<p class=\"import-Normal\">In computer programming we accept a problem to solve and develop an algorithm that can serve as a general solution. Once we have such a solution, we can use our computer to automate the execution. Programming is a skill that allows a competent programmer to take an algorithm and represent it in a notation (a program) that can be followed by a computer. These programs are written in <strong>programming languages <\/strong>(such as Python). Writing a correct and valid algorithm to solve a computational problem is key to writing good code. Learn to <em>Think First<\/em> and coding will come naturally!<\/p>\r\n\r\n<h1>The Process of Computational Problem Solving<\/h1>\r\n<p class=\"import-Normal\">Computational problem solving does not simply involve the act of computer programming. It is a process, with programming being only one of the steps. Before a program is written, a design for the program must be developed (the algorithm). And before a design can be developed, the problem to be solved must be well understood. Once written, the program must be thoroughly tested. These steps are outlined in Figure 5.<\/p>\r\n\r\n[caption id=\"\" align=\"aligncenter\" width=\"577\"]<img class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232245\/image6.png\" alt=\"image\" width=\"577\" height=\"567\" \/> Figure 5: Process of Computational Problem Solving<span class=\"import-FootnoteReference\">[footnote]Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, pp17-18.[\/footnote]<\/span>[\/caption]\r\n<h1>Values and Variables<\/h1>\r\n<p class=\"import-Normal\">A value is one of the basic things computer programs works with, like a password or a number of errors.<\/p>\r\n<p class=\"import-Normal\">Values belong to different types: 21 is an integer (like the number of errors), and \u2018comp15\u2019 is a string of characters (like the password). Python lets you give names to <em class=\"import-Emphasis\">values<\/em> <em class=\"import-Emphasis\">giving us the ability to generalize our algorithms.<\/em><\/p>\r\n<p class=\"import-Normal\">One of the most powerful features of a programming language is the ability to use variables. A variable is simply a name that refers to a value as shown below,<\/p>\r\n\r\n<table style=\"border-collapse: collapse;width: 100%\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 50%\"><code>errors = 21<\/code><\/td>\r\n<td style=\"width: 50%\">variable <code>errors<\/code> is assigned the value 21<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 50%\"><code>password = \u2018comp15\u2019<\/code><\/td>\r\n<td style=\"width: 50%\">\u00a0variable <code>password<\/code> is assigned the value \u2018comp15\u2019<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nWhenever the variable errors appears in a calculation the current value of the variable is used.\r\n<table style=\"border-collapse: collapse;width: 100%\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 50%\"><code>errors = 21<\/code><\/td>\r\n<td style=\"width: 50%\">variable <code>errors<\/code> is assigned the value 21<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 50%\"><code>errors = errors + 1<\/code><\/td>\r\n<td style=\"width: 50%\">variable <code>errors<\/code> is assigned the value of 21+1 (22)<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"import-Normal\">We need some way of storing information (i.e. the number of errors or the password) and manipulate them as well. This is where variables come into the picture. Variables are exactly what the name implies - their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer's memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names.<\/p>\r\n<p class=\"import-Normal\">Programmers generally choose names for their variables that are meaningful and document what the variable is used for. It is a good idea to begin variable names with a lowercase letter . The underscore character (_) can appear in a name and is often used in names with multiple words.<\/p>\r\n<p class=\"import-Normal\">A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of mathematical equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or something graphical, like processing user input on an ATM device.<\/p>\r\n\r\n<h1>What is a Program?<\/h1>\r\n[caption id=\"\" align=\"alignright\" width=\"320\"]<img class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232247\/image7.jpg\" alt=\"image\" width=\"320\" height=\"240\" \/> Figure 6: \u201c<a href=\"https:\/\/flic.kr\/p\/86bi4j\">Python Code<\/a>\u201d by <a href=\"https:\/\/www.flickr.com\/photos\/nyuhuhuu\/\">nyuhuhuu<\/a> is licensed under <a href=\"https:\/\/creativecommons.org\/licenses\/by\/2.0\/\">CC-BY 2.0<\/a>[\/caption]\r\n<p class=\"import-Normal\">The details look different in different computer programming languages, but there are some low-level conceptual patterns (constructs) that we use to write all programs. These constructs are not just for Python programs, they are a part of every programming language.<\/p>\r\n<p class=\"import-Normal\"><strong>input<\/strong> Get data from the \u201coutside world\u201d. This might be reading data from a file, or even some kind of sensor like a microphone or GPS. In our initial algorithms and programs, our input will come from the user typing data on the keyboard.<\/p>\r\n<p class=\"import-Normal\"><strong>output<\/strong> Display the results of the program on a screen or store them in a file or perhaps write them to a device like a speaker to play music or speak text.<\/p>\r\n<p class=\"import-Normal\"><strong>sequential<\/strong> <strong>execution<\/strong> Perform statements one after another in the order they are encountered in the script.<\/p>\r\n<p class=\"import-Normal\"><strong>conditional<\/strong> <strong>execution<\/strong> Checks for certain conditions and then executes or skips a sequence of statements.<\/p>\r\n<p class=\"import-Normal\"><strong>repeated<\/strong> <strong>execution<\/strong> Perform some set of statements repeatedly, usually with some variation.<\/p>\r\n<p class=\"import-Normal\"><strong>reuse<\/strong> Write a set of instructions once and give them a name and then reuse those instructions as needed throughout your program.<\/p>\r\n<p class=\"import-Normal\">Believe it or not, that\u2019s pretty much all there is to it. Every computer application you\u2019ve ever used, no matter how complicated, is made up of constructs that look pretty much like these. So you can think of programming as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of these basic constructs. The \u201cart\u201d of writing a program is composing and weaving these basic elements together many times over to produce something that is useful to its users.<\/p>\r\n\r\n<h1>Computational Problem\u00a0Design Using the Basic Programming Constructs<\/h1>\r\n<p class=\"import-Normal\">The key to better algorithm design and thus to programming lies in limiting the <strong>control structure<\/strong> to only three constructs as shown below.<\/p>\r\n\r\n<ol>\r\n \t<li>The Sequence structure (sequential execution)<\/li>\r\n \t<li>The Decision, Selection or Control structure (conditional execution)<\/li>\r\n \t<li>Repetition or Iteration Structure (repeated execution)<\/li>\r\n<\/ol>\r\n[caption id=\"\" align=\"aligncenter\" width=\"712\"]<img class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232249\/image8.png\" alt=\"image\" width=\"712\" height=\"401\" \/> Figure 7: the 3 Programming Constructs[\/caption]\r\n\r\n<small>\u00a0<\/small>Let us look at some examples for the sequential control and the selection control.\r\n<h3><a name=\"_Toc529437417\"><\/a>Sequential Control Example<\/h3>\r\n<p class=\"import-Normal\">The following algorithm is an example of <strong>sequential control<\/strong>.<\/p>\r\n\r\n<div class=\"textbox\">\r\n\r\n<strong>Problem<\/strong>: Given two numbers, return the sum and the product of the two numbers.\r\n\r\n<strong>Inputs<\/strong>: Two numbers.\r\n\r\n<strong>Outputs<\/strong>: The sum and the product.\r\n<ol>\r\n \t<li class=\"import-Normal\"><code>Start<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>display \u201cInput two numbers\u201d<\/code><\/li>\r\n \t<li style=\"list-style-type: none\"><\/li>\r\n \t<li class=\"import-Normal\"><code>sum = number1 + number2<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>print \u201cThe sum is \u201c, sum<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>product = number1 * number2<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>print \u201cThe product is \u201c, product<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>End<\/code><\/li>\r\n<\/ol>\r\n<\/div>\r\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\r\n\r\n<ul>\r\n \t<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\r\n \t<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\r\n \t<li><em>Is it guaranteed to terminate?<\/em> Yes. Sequential control, by its nature, always ends.<\/li>\r\n \t<li><em>Is it general for any input?<\/em> Yes. Any two numbers work in this design.<\/li>\r\n \t<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Here is an example of three different test cases that are used to verify the algorithm.<\/p>\r\n\r\n<div style=\"margin: auto\">\r\n<table style=\"height: 330px;width: 702px\">\r\n<tbody>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 167.35px;height: 57px\">\r\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 256.3px;height: 57px\">\r\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 232.483px;height: 57px\">\r\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 167.35px;height: 82px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 256.3px;height: 82px\">\r\n<p class=\"import-Normal\">numbers 0 and 859<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 232.483px;height: 82px\">\r\n<p class=\"import-Normal\">sum is 859<br style=\"clear: both\" \/>product is 0<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 167.35px;height: 79px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 256.3px;height: 79px\">\r\n<p class=\"import-Normal\">numbers -5 and 10<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 232.483px;height: 79px\">\r\n<p class=\"import-Normal\">sum is 5<br style=\"clear: both\" \/>product is -50<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 167.35px;height: 47px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 256.3px;height: 47px\">\r\n<p class=\"import-Normal\">numbers 12 and 3<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 232.483px;height: 47px\">\r\n<p class=\"import-Normal\">sum is 15<br style=\"clear: both\" \/>product is 36<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<h3>Selection Control<\/h3>\r\n<p class=\"import-Normal\">The following two algorithms are examples of <strong>selection<\/strong><strong> control<\/strong> which uses the \u2018IF\u2019 statement in most programming languages.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"import-Normal\"><strong>Problem<\/strong>: Given two numbers, the user chooses to either multiply, add or subtract the two numbers. Return the value of the chosen calculation.<\/p>\r\n<p class=\"import-Normal\"><strong>Inputs<\/strong>: Two numbers and calculation option.<\/p>\r\n<p class=\"import-Normal\"><strong>Outputs<\/strong>: The value of the chosen calculation.<\/p>\r\n\r\n<div class=\"textbox shaded\">\r\n\r\nThe relational (or comparison) operators used in selection control are:\r\n\r\n= is equal to\r\n\r\n&gt; is greater than\r\n\r\n&lt; is less than\r\n\r\n&gt;= is greater than or equal\r\n\r\n&lt;= is less than or equal\r\n\r\n&lt;&gt; is not equal to\r\n\r\n<\/div>\r\n<ol>\r\n \t<li class=\"import-Normal\"><code>start<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>display \u201cchoose one of the following\u201d<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>display \u201cm for multiply\u201d<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>display \u201ca for add\u201d<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>display \u201cs for subtract\u201d<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>accept choice<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>display \u201cinput two numbers you want to use\u201d<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>accept number1, number2<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>if choice = m then answer= number1 * number2<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>if choice = a then answer= number1 + number2<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>if choice = s then answer= number1 -number212. if choice is not m, a, or s then answer is NONE<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>display answer<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>stop<\/code><\/li>\r\n<\/ol>\r\n<\/div>\r\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\r\n\r\n<ul>\r\n \t<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\r\n \t<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\r\n \t<li><em>Is it guaranteed to terminate?<\/em> Yes. The input is of finite length, so after accepting the user\u2019s choice and the two numbers the algorithm will stop.<\/li>\r\n \t<li><em>Is it general for any input?<\/em> Yes. Any two numbers work in this design and only a choice of a\u2019m\u2019, \u2018a\u2019, or \u2018s\u2019 will result in numeric output.<\/li>\r\n \t<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Here is an example of three different test cases that are used to verify the algorithm.<\/p>\r\n\r\n<div style=\"margin: auto\">\r\n<table style=\"height: 333px;width: 603px\">\r\n<tbody>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 136.067px;height: 73px\">\r\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 230.033px;height: 73px\">\r\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 191.033px;height: 73px\">\r\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 136.067px;height: 99px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 230.033px;height: 99px\">\r\n<p class=\"import-Normal\">choice \u2018a\u2019<br style=\"clear: both\" \/>numbers -12 and 32<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 191.033px;height: 99px\">\r\n<p class=\"import-Normal\">answer is 20<br style=\"clear: both\" \/>terminate<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 136.067px;height: 99px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 230.033px;height: 99px\">\r\n<p class=\"import-Normal\">choice \u2018s\u2019<br style=\"clear: both\" \/>numbers -2012 and 0<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 191.033px;height: 99px\">\r\n<p class=\"import-Normal\">answer is 2012<br style=\"clear: both\" \/>terminate<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 136.067px;height: 60px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 230.033px;height: 60px\">\r\n<p class=\"import-Normal\">choice \u2018**\u2019<br style=\"clear: both\" \/>numbers 8 and 4<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 191.033px;height: 60px\">\r\n<p class=\"import-Normal\">answer is NONE<br style=\"clear: both\" \/>terminate<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<p class=\"import-Normal\">This example uses an extension of the simple selection control structure we just saw and is referred to as the \u2018IF-ELSE\u2019 structure.<\/p>\r\n\r\n<div class=\"textbox\">\r\n\r\n&nbsp;\r\n\r\n<strong>Problem<\/strong>: Accept from the user a positive integer value representing a salary amount, return tax due based on the salary amount.\r\n\r\n<strong>Inputs<\/strong>: One positive integer number.\r\n\r\n<strong>Outputs<\/strong>: The calculated tax amount.\r\n<div class=\"textbox shaded\">\r\n\r\nThe relational (or comparison) operators used in selection control are:\r\n\r\n= is equal to\r\n\r\n&gt; is greater than\r\n\r\n&lt; is less than\r\n\r\n&gt;= is greater than or equal\r\n\r\n&lt;= is less than or equal\r\n\r\n&lt;&gt; is not equal to\r\n\r\n<\/div>\r\n<ol>\r\n \t<li><code>start<\/code><\/li>\r\n \t<li><code>accept salary<\/code><\/li>\r\n \t<li><code>If salary &lt; 50000 then<\/code><\/li>\r\n \t<li><code>Tax = 0 Else<\/code><\/li>\r\n \t<li><code>If salary &gt; 50000 AND salary &lt; 100000 then<\/code><\/li>\r\n \t<li><code>Tax = 50000 * 0.05 Else<\/code><\/li>\r\n \t<li><code>Tax = 100000 * 0.30<\/code><\/li>\r\n \t<li><code>End IF<\/code><\/li>\r\n \t<li><code>display Tax<\/code><\/li>\r\n \t<li><code>stop<\/code><\/li>\r\n<\/ol>\r\n<\/div>\r\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\r\n\r\n<ul>\r\n \t<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\r\n \t<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\r\n \t<li><em>Is it guaranteed to terminate?<\/em> Yes. The input is of finite length, so after accepting the user\u2019s number, even if it is negative, the algorithm will stop.<\/li>\r\n \t<li><em>Is it general for any input?<\/em> Yes. Any number entered in this design will work.<\/li>\r\n \t<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Here is an example of three different test cases that are used to verify the algorithm.<\/p>\r\n\r\n<div style=\"margin: auto\">\r\n<table style=\"height: 365px;width: 605px\">\r\n<tbody>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 150.283px\">\r\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 199.45px\">\r\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 209.4px\">\r\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 150.283px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 199.45px\">\r\n<p class=\"import-Normal\">salary of 0<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 209.4px\">\r\n<p class=\"import-Normal\">tax is 0<br style=\"clear: both\" \/>terminate<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 150.283px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 199.45px\">\r\n<p class=\"import-Normal\">salary of 75000<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 209.4px\">\r\n<p class=\"import-Normal\">tax is 2500<br style=\"clear: both\" \/>terminate<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 150.283px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 199.45px\">\r\n<p class=\"import-Normal\">salary of 120000<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 209.4px\">\r\n<p class=\"import-Normal\">tax is 30000<br style=\"clear: both\" \/>terminate<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<h3>Iterative Control Examples<\/h3>\r\n<p class=\"import-Normal\">The third programming control is the iterative or, also referred to as, the repetition structure. This control structure causes certain steps to be repeated in a sequence a specified number of times or until a condition is met. This is what is called a \u2018loop\u2019 in programming<\/p>\r\n<p class=\"import-Normal\">In all programming languages there are generally two options: an indefinite loop (the Python \u2018WHILE\u2019 programming statement) and a definite loop (the Python \u2018FOR\u2019 programming statement). We can use these two constructs, WHILE and FOR, for iterations or loops in our algorithms.<\/p>\r\n\r\n<div class=\"textbox\">\r\n\r\n<strong>Note for Reader: <\/strong>\r\nA <strong>definite loop<\/strong> is where we know exactly the number of times the loop\u2019s body will be executed. Definite iteration is usually best coded as a Python for loop. An <strong>indefinite loop<\/strong> is where we do not know before entering the body of the loop the exact number of iterations the loop will perform. The loop just keeps going until some condition is met. A while statement is used in this case.\r\n\r\n<\/div>\r\n<p class=\"import-Normal\">The following algorithm is an example of <strong>iterative<\/strong><strong> control<\/strong> using<strong> WHILE<\/strong>.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"import-Normal\"><strong>Problem<\/strong>: Print each keyboard character the users types in until the user chooses the \u2018q\u2019 (for \u2018quit\u2019) character.<\/p>\r\n<p class=\"import-Normal\"><strong>Inputs<\/strong>: A series of individual characters.<\/p>\r\n<p class=\"import-Normal\"><strong>Outputs<\/strong>: Each character typed in by the user.<\/p>\r\n\r\n<ol>\r\n \t<li class=\"import-Normal\"><code>Start<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>initialize (set) letter = \u2018a\u2019<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>WHILE letter &lt;&gt; \u2018q\u2019<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>ACCEPT letter<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>DISPLAY \u201cThe character you typed is\u201d, letter<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>end WHILE<\/code><\/li>\r\n \t<li class=\"import-Normal\"><code>Stop<\/code><\/li>\r\n<\/ol>\r\n<\/div>\r\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\r\n\r\n<ul>\r\n \t<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\r\n \t<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\r\n \t<li><em>Is it guaranteed to terminate?<\/em> Yes. The input is of finite length, so after accepting the user\u2019s keyboard character, even if it is not a letter, the algorithm will stop.<\/li>\r\n \t<li><em>Is it general for any input?<\/em> Yes. Any keyboard character entered in this design will work.<\/li>\r\n \t<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Here is an example of three different test cases that are used to verify the algorithm.<\/p>\r\n\r\n<div style=\"margin: auto\">\r\n<table style=\"height: 373px;width: 632px\">\r\n<tbody>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 134.667px\">\r\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 144.083px\">\r\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 307.383px\">\r\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 134.667px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 144.083px\">\r\n<p class=\"import-Normal\">letter \u2018z\u2019<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 307.383px\">\r\n<p class=\"import-Normal\">The character you typed is z. <br style=\"clear: both\" \/>Ask for another letter.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 134.667px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 144.083px\">\r\n<p class=\"import-Normal\">letter \u20188\u2019<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 307.383px\">\r\n<p class=\"import-Normal\">The character you typed is 8<br style=\"clear: both\" \/>Ask for another letter.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 134.667px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 144.083px\">\r\n<p class=\"import-Normal\">letter \u2018q\u2019<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 307.383px\">\r\n<p class=\"import-Normal\">The character you typed is q. <br style=\"clear: both\" \/>Terminate.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<p class=\"import-Normal\">The following algorithm is an example of <strong>iterative<\/strong><strong> control<\/strong> using<strong> FOR<\/strong>. This statement is used when the number of iterations is known in advance.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"import-Normal\"><strong>Problem<\/strong>: Ask the user how many words they want to enter then print the words entered by the user.<\/p>\r\n<p class=\"import-Normal\"><strong>Inputs<\/strong>: Number of words to be entered; this value must be a positive integer greater than zero. Individual words.<\/p>\r\n<p class=\"import-Normal\"><strong>Outputs<\/strong>: Each word typed in by the user.<\/p>\r\n\r\n<ol>\r\n \t<li class=\"import-Normal\">\r\n<pre>Start<\/pre>\r\n<\/li>\r\n \t<li class=\"import-Normal\">\r\n<pre>accept num_words (must be at least one)<\/pre>\r\n<\/li>\r\n \t<li class=\"import-Normal\">\r\n<pre>repeat num_words times (FOR 1 to num_words)<\/pre>\r\n<\/li>\r\n \t<li class=\"import-Normal\">\r\n<pre>accept word<\/pre>\r\n<\/li>\r\n \t<li class=\"import-Normal\">\r\n<pre>DISPLAY \u201cThe word you entered is\u201d, word<\/pre>\r\n<\/li>\r\n \t<li class=\"import-Normal\">\r\n<pre>end FOR<\/pre>\r\n<\/li>\r\n \t<li class=\"import-Normal\">\r\n<pre>Stop<\/pre>\r\n<\/li>\r\n<\/ol>\r\n<\/div>\r\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\r\n\r\n<ul>\r\n \t<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\r\n \t<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\r\n \t<li><em>Is it guaranteed to terminate?<\/em> Yes. The input is of finite length, so after accepting the user\u2019s number of words to enter and any characters typed on the keyboard, even if it is not a \u2018word\u2019 per say, the algorithm will stop.<\/li>\r\n \t<li><em>Is it general for any input?<\/em> Yes. Any positive integer greater than zero and any size \u2018word\u2019 will work.<\/li>\r\n \t<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Here is an example of two different test cases that are used to verify the algorithm.<\/p>\r\n\r\n<div style=\"margin: auto\">\r\n<table style=\"height: 416px;width: 680px\">\r\n<tbody>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 135.517px\">\r\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 147.7px\">\r\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 350.917px\">\r\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 135.517px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 147.7px\">\r\n<p class=\"import-Normal\">num_words 1<br style=\"clear: both\" \/>word \u2018code\u2019<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 350.917px\">\r\n<p class=\"import-Normal\">The word you entered is \u2018code\u2019. <br style=\"clear: both\" \/>Terminate.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 135.517px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 147.7px\">\r\n<p class=\"import-Normal\">num_words 3<br style=\"clear: both\" \/>word \u2018coding\u2019<br style=\"clear: both\" \/><br style=\"clear: both\" \/>word \u2018is\u2019<br style=\"clear: both\" \/><br style=\"clear: both\" \/><br style=\"clear: both\" \/>word \u2018fun\u2019<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 350.917px\">\r\n<p class=\"import-Normal\">The word you entered is \u2018coding\u2019. <br style=\"clear: both\" \/>Ask for another word.<br style=\"clear: both\" \/><br style=\"clear: both\" \/>The word you entered is \u2018is\u2019. <br style=\"clear: both\" \/>Ask for another word.<br style=\"clear: both\" \/><br style=\"clear: both\" \/>The word you entered is \u2018fun\u2019. <br style=\"clear: both\" \/>Terminate.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<div class=\"textbox\"><strong>Note to Reader:\r\n<\/strong>\r\nAn <strong>infinite loop<\/strong> is a loop that executes its block of statements repeatedly until the user forces the program to quit. Once the program flow enters the loop\u2019s body it cannot escape. Infinite loops sometimes are by design. For example, a long-running server application like a Web server may need to continuously check for incoming connections. The Web server can perform this checking within a loop that runs indefinitely.Beginning programmers, unfortunately, all too often create infinite loops by accident, and these infinite loops represent logic errors in their programs<\/div>\r\n<h1>The Role of Programming in the Field of Informatics<\/h1>\r\n[caption id=\"\" align=\"alignright\" width=\"517\"]<img src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232252\/image9.jpg\" alt=\"image\" width=\"517\" height=\"216\" \/> Figure8: iPhone apps by Jaap Arriens\/NurPhoto via Getty Images (abcnews.go.com)[\/caption]\r\n<p class=\"import-Normal\">You see computer programming in use every day. When you use Google or your smartphone, or watch a movie with special effects, there is programing at work. When you order a product over the Internet, there is code in the web site, in the cryptography used to keep your credit card number secure, and in the way that UPS routes their delivery vehicle to get your order to you as quickly as possible.<\/p>\r\n<p class=\"import-Normal\">Programming is indeed important to an informatics professional as they are interested in finding solutions for a wide variety of computational problems involving data.<\/p>\r\n<p class=\"import-Normal\">When you Google the words \u201cpie recipe,\u201d Google reports that it finds approximately 38 million pages, ranked in order of estimated relevance and usefulness. Facebook has approximately 1 billion active users who generate over 3 billion comments and \u201cLikes\u201d each day. GenBank, a national database of DNA sequences used by biologists and medical researchers studying genetic diseases, has over 100 million genetic sequences with over 100 billion DNA base pairs. According to the International Data Corporation, by 2020 the digital universe \u2013 the data we create and copy annually \u2013 will reach 44 zettabytes, or 44 trillion gigabytes.<\/p>\r\n\r\n\r\n[caption id=\"\" align=\"aligncenter\" width=\"595\"]<img src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232255\/image10.jpg\" alt=\"image\" width=\"595\" height=\"277\" \/> Figure 9: The Digital Universe (<a href=\"http:\/\/www.emc.com\/leadership\/digital-universe\/2014iview\/images\">www.emc.com\/leadership\/digital-universe\/2014iview\/images<\/a>)[\/caption]\r\n\r\n<small>\u00a0<\/small>Doing meaningful things with data is challenging, even if we\u2019re not dealing with millions or billions of things. In this book, we will be working with smaller sets of data. But much of what we\u2019ll do will be applicable to very large amounts of data too.\r\n<h1>Unit Summary<\/h1>\r\n<p class=\"import-Normal\" style=\"margin-left: 18pt\">Computational Thinking is the thought processes involved in formulating a problem and expressing its solution in a way that a computer\u2014human or machine\u2014can effectively carry out.<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 18pt\">Computational Thinking is what comes before any computing technology\u2014thought of by a human, knowing full well the power of automation.<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 18pt\">Writing a correct and valid algorithm to solve a computational problem is key to writing good code.<\/p>\r\n\r\n<ol>\r\n \t<li class=\"import-Normal\">First, understand the problem\r\n<ol type=\"a\">\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">What are the inputs?<\/em><\/li>\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">What are the outputs (or results)?<\/em><\/li>\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Can we break the problem into parts?<\/em><\/li>\r\n<\/ol>\r\n<\/li>\r\n \t<li class=\"import-Normal\">Second, design the process (write the <strong style=\"text-align: initial;font-size: 1em\">algorithm<\/strong><span style=\"text-align: initial;font-size: 1em\">!).<\/span>\r\n<ol type=\"a\">\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Think about the connections between the input &amp; output.<\/em><\/li>\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Consider designing \u2018backwards\u2019.<\/em><\/li>\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Have you seen the problem before? In a slightly different form?<\/em><\/li>\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Can you solve part of the problem?<\/em><\/li>\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Did you use all the inputs?<\/em><\/li>\r\n<\/ol>\r\n<\/li>\r\n \t<li class=\"import-Normal\">Third, test your design.\r\n<ol type=\"a\">\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Can you test it on a variety of inputs?<\/em><\/li>\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Can you think of how you might write the algorithm differently if you had to start again?<\/em><\/li>\r\n<\/ol>\r\n<\/li>\r\n \t<li class=\"import-Normal\">Last, write the program (we will be addressing this in our next Unit).\r\n<ol type=\"a\">\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Does it solve the problem? Does it meet all the requirements? Is the output correct?<\/em><\/li>\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Does it terminate?<\/em><\/li>\r\n \t<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Is it general for all cases?<\/em><\/li>\r\n<\/ol>\r\n<\/li>\r\n<\/ol>\r\n<h1>Practice Problems<\/h1>\r\n<ol>\r\n \t<li>Write about a process in your life (e.g. driving to the mall, walking to class, etc.) and estimate the number of steps necessary to complete the task. Would you consider this a complex or simple task? What happens if you scale that task (e.g. driving two states away to the mall)? Is your method the most efficient? Can you come up with a more efficient way?<\/li>\r\n \t<li>Given five cards randomly placed in a row like the ones below write an algorithm to sort the cards. How would your algorithm change if there were 10 cards? What about 100 cards?\r\n<img class=\"aligncenter\" style=\"text-align: center;font-size: 1em\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232257\/image11.png\" alt=\"image\" width=\"301.333333333333px\" height=\"64.8666666666667px\" \/><\/li>\r\n \t<li>Write an algorithm to find the average of 25 test grades out of a possible 100 points.<\/li>\r\n \t<li>If you are given three sticks, you may or may not be able to arrange them in a triangle. For example, if one of the sticks is 12 inches long and the other two are one inch long, it is clear that you will not be able to get the short sticks to meet in the middle. For any three lengths, there is a simple test to see if it is possible to form a triangle: \u201cIf any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can.\u201dWrite an algorithm that accepts three integers as arguments, and that displays either \u201cYes\u201d or \u201cNo,\u201d depending on whether you can or cannot form a triangle from sticks with the given lengths.<\/li>\r\n \t<li>ROT13 is a weak form of encryption that involves \u201crotating\u201d each letter in a word by 13 places. To rotate a letter means to shift it through the alphabet, wrapping around to the beginning if necessary, so \u2018A\u2019 shifted by 3 is \u2018D\u2019 and \u2018Z\u2019 shifted by 1 is \u2018A\u2019. <br style=\"clear: both\" \/>Write an algorithm that accepts a word and an integer from the user, and that prints a new encrypted word that contains the letters from the original word \u201crotated\u201d by the given amount (the integer input). For example, \u201ccheer\u201d rotated by 7 is \u201cjolly\u201d and \u201cmelon\u201d rotated by \u221210 is \u201ccubed.\u201d<\/li>\r\n \t<li>Write an algorithm to accept a score between 0.0 and 1.0. If the score is out of range, display an error message. If the score is between 0.0 and 1.0, display a grade using the following table:\r\n<table style=\"border-collapse: collapse;width: 100%\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 1.43062%\"><strong>Score<\/strong><\/td>\r\n<td style=\"width: 1.43062%\"><strong>Grade<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 1.43062%\">&gt;= 0.9<\/td>\r\n<td style=\"width: 1.43062%\">A<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 1.43062%\">&gt;= 0.8<\/td>\r\n<td style=\"width: 1.43062%\">B<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 1.43062%\">&gt;= 0.7<\/td>\r\n<td style=\"width: 1.43062%\">C<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 1.43062%\">&gt;= 0.6<\/td>\r\n<td style=\"width: 1.43062%\">D<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 1.43062%\">&lt; 0.6<\/td>\r\n<td style=\"width: 1.43062%\">E<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/li>\r\n \t<li>Write an algorithm which repeatedly accepts numbers until the user enters \u201cdone\u201d. Once \u201cdone\u201d is entered, display the total sum of all the numbers, the count of numbers entered, and the average of all the numbers.<\/li>\r\n \t<li>Write an algorithm that sums a series of ten positive integers entered by the user excluding all numbers greater than 100. Display the final sum.<\/li>\r\n<\/ol>","rendered":"<h1>Learning Objectives<\/h1>\n<ul>\n<li>Explain what we mean by \u201cComputational Thinking\u201d.<\/li>\n<li>Describe the problem being solved in a computational algorithm.<\/li>\n<li>Explain the process for generating computational algorithms.<\/li>\n<li>Generate and test algorithms to solve computational problems.<\/li>\n<li>Evaluate computational algorithms for exactness, correctness, termination, generalizability and understandability.<\/li>\n<li>Explain the role of programming in the field of Informatics.<\/li>\n<\/ul>\n<h1>Introduction<\/h1>\n<p class=\"import-Normal\">The goal of this book is to teach you to solve computational problems and to think like an engineer. Computational problems are problems that can be solved by the use of computations (a computation is what you do when you calculate something). Engineers are people who solve problems &#8211; they invent, design, analyze, build and test &#8220;things&#8221; to fulfill objectives and requirements. The single most important skill for you to learn is problem solving. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of learning to program is an excellent opportunity to practice problem-solving skills.<\/p>\n<p class=\"import-Normal\">This book strives to prepare you to write well-designed computer programs that solve interesting problems involving data.<\/p>\n<h1>Computational Thinking<\/h1>\n<div style=\"width: 470px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232232\/image3.jpg\" alt=\"computational thinking chart\" width=\"460\" height=\"306\" \/><\/p>\n<p class=\"wp-caption-text\">Figure 1:\u00a0&#8220;The seven components to computational thinking&#8221;(www.ignitemyfutureinschool.org\/about)<\/p>\n<\/div>\n<p class=\"import-Normal\"><em>Computational Thinking<\/em> is the thought processes involved in understanding a problem and expressing its solution in a way that a computer can effectively carry out. Computational thinking involves solving problems, designing systems, and understanding human behavior (e.g. what the user needs or wants) \u2013 thinking like an engineer. Computational thinking is a fundamental skill for everyone, not just for programmers because computational thinking is what comes <em>before<\/em> any computing technology.<a class=\"footnote\" title=\"Wing, Jeannette M. &quot;Computational thinking.&quot; Communications of the ACM 49.3 (2006): 33-35.\" id=\"return-footnote-35-1\" href=\"#footnote-35-1\" aria-label=\"Footnote 1\"><sup class=\"footnote\">[1]<\/sup><\/a><small><\/small><\/p>\n<p class=\"import-Normal\">Computer science is the study of computation \u2014 what can be computed and how to compute it whereas computational thinking is:<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>Conceptualizing<\/strong>, not programming. Computer science is not only computer programming. Thinking like a computer scientist means more than being able to program a computer. It requires thinking at multiple levels of abstraction;<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>Fundamental<\/strong>, not rote skill. A fundamental skill is something every human being must know to function in modern society. Rote means a mechanical routine;<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>A way that humans, not computers, think<\/strong>. Computational thinking is a way humans solve problems; it is not trying to get humans to think like computers. Computers are dull and boring; humans are clever and imaginative. We humans make computers exciting. Equipped with computing devices, we use our cleverness to tackle problems we would not dare take on before the age of computing and build systems with functionality limited only by our imaginations;<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>Complements and combines mathematical and engineering thinking<\/strong>. Computer science inherently draws on mathematical thinking, given that, like all sciences, its formal foundations rest on mathematics. Computer science inherently draws on engineering thinking, given that we build systems that interact with the real world;<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>Ideas<\/strong>, not artifacts. It\u2019s not just the software and hardware artifacts we produce that will be physically present everywhere and touch our lives all the time, it will be the computational concepts we use to approach and solve problems, manage our daily lives, and communicate and interact with other people;<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\"><strong>For everyone, everywhere<\/strong>. Computational thinking will be a reality when it is so integral to human endeavors it disappears as an explicit philosophy.<a class=\"footnote\" title=\"Wing, Jeannette M. &quot;Computational thinking.&quot; Communications of the ACM 49.3 (2006): 33-35.\" id=\"return-footnote-35-2\" href=\"#footnote-35-2\" aria-label=\"Footnote 2\"><sup class=\"footnote\">[2]<\/sup><\/a><\/p>\n<h1>Algorithms<\/h1>\n<div id=\"attachment_278\" style=\"width: 379px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-278\" class=\"wp-image-278\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232235\/Areyouhappy.jpg\" alt=\"\" width=\"369\" height=\"529\" \/><\/p>\n<p id=\"caption-attachment-278\" class=\"wp-caption-text\">Figure 2 &#8220;Are you happy?&#8221; by Typcut <a href=\"http:\/\/www.typcut.com\/headup\/are-you-happy\">http:\/\/www.typcut.com\/headup\/are-you-happy<\/a><\/p>\n<\/div>\n<p class=\"import-Normal\">An algorithm specifies a series of steps that perform a particular computation or task. Throughout this book we\u2019ll examine a number of different algorithms to solve a variety of computational problems.<\/p>\n<p class=\"import-Normal\">Algorithms resemble recipes. Recipes tell you how to accomplish a task by performing a number of steps. For example, to bake a cake the steps are: preheat the oven; mix flour, sugar, and eggs thoroughly; pour into a baking pan; set the timer and bake until done.<\/p>\n<p class=\"import-Normal\">However, \u201calgorithm\u201d is a technical term with a more specific meaning than \u201crecipe\u201d, and calling something an algorithm means that the following properties are all true:<\/p>\n<ol>\n<li>An algorithm is an unambiguous description that makes clear what has to be implemented in order to solve the problem. In a recipe, a step such as \u201cBake until done\u201d is ambiguous because it doesn\u2019t explain what \u201cdone\u201d means. A more explicit description such as \u201cBake until the cheese begins to bubble\u201d is better. In a computational algorithm, a step such as \u201cChoose a large number\u201d is vague: what is large? 1 million, 1 billion, or 100? Does the number have to be different each time, or can the same number be used again?<\/li>\n<li>An algorithm expects a defined set of inputs. For example, it might require two numbers where both numbers are greater than zero. Or it might require a word,<span style=\"text-align: initial;font-size: 1em\"> or a <\/span>list<span style=\"text-align: initial;font-size: 1em\"> customer names.<\/span><\/li>\n<li>An algorithm produces a defined set of outputs. It might output the larger of the two numbers, an all-uppercase version of a word, or a sorted version of the list of names.<\/li>\n<li>An algorithm is guaranteed to terminate and produce a result, always stopping after a finite time. If an algorithm could potentially run forever, it wouldn\u2019t be very useful because you might never get an answer.<\/li>\n<li>Must be general for any input it is given. Algorithms solve <em style=\"text-align: initial;font-size: 1em\">general<\/em><span style=\"text-align: initial;font-size: 1em\"> problems (determine if a password is valid); they are of little use if they only solve a <\/span><em style=\"text-align: initial;font-size: 1em\">specific<\/em><span style=\"text-align: initial;font-size: 1em\"> problem (determine if \u2018comp15\u2019 is a valid password)<\/span><\/li>\n<li>It is at the right level of detail\u2026..the person or device executing the instruction know how to accomplish the instruction without any extra information.<\/li>\n<\/ol>\n<p class=\"import-Normal\">Once we know it\u2019s possible to solve a problem with an algorithm, a natural question is whether the algorithm is the best possible one. Can the problem be solved more quickly or efficiently?<\/p>\n<p class=\"import-Normal\">The first thing you need to do before designing an algorithm is to understand completely the problem given. Read the problem\u2019s description carefully, then read it again. Try sketching out by hand some examples of how the problem can be solved. Finally consider any special cases and design your algorithm to address them.<\/p>\n<p class=\"import-Normal\"><strong>An algorithm does not solve a problem rather it gives you a series of steps that, if executed correctly, will result in a solution to a problem.<\/strong><\/p>\n<h1>An Example Algorithm<\/h1>\n<p class=\"import-Normal\">Let us look at a very simple algorithm called find_max.<\/p>\n<p class=\"import-Normal\"><strong>Problem<\/strong>: Given a list of positive numbers, return the largest number on the list.<\/p>\n<p class=\"import-Normal\"><strong>Inputs<\/strong>: A list of positive numbers. This list must contain at least one number. (Asking for the largest number in a list of no numbers is not a meaningful question.)<\/p>\n<p class=\"import-Normal\"><strong>Outputs<\/strong>: A number, which will be the largest number in the list.<\/p>\n<p class=\"import-Normal\"><strong>Algorithm<\/strong>:<\/p>\n<ol>\n<li>Start<\/li>\n<li>Accept a list of positive numbers; set to<code> nums_list<\/code><\/li>\n<li>Set <code>max_number <\/code>to 0.<\/li>\n<li>For each number in<code> nums_list, <\/code>compare it to max_number.\n<ol type=\"a\">\n<li>If the number is larger,<code> set max_number <\/code>to the larger number.<\/li>\n<\/ol>\n<\/li>\n<li><code>max_number <\/code>is now set to the largest number in the list of positive numbers,<code> nums_list.<\/code><\/li>\n<li>End<\/li>\n<\/ol>\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\n<ul>\n<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\n<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\n<li><em>Is it guaranteed to terminate?<\/em> Yes. The list nums_list is of finite length, so after looking at every element of the list the algorithm will stop.<\/li>\n<li><em>Is it general for any input?<\/em> Yes. A list of any set of positive numbers works.<\/li>\n<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected<\/li>\n<\/ul>\n<div id=\"attachment_84\" style=\"width: 619px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-84\" class=\"wp-image-84\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232237\/Screen-Shot-2018-07-17-at-8.43.09-AM.png\" alt=\"Figure 3: Example Algorithm\" width=\"609\" height=\"235\" \/><\/p>\n<p id=\"caption-attachment-84\" class=\"wp-caption-text\">Figure 3: Example Algorithm<\/p>\n<\/div>\n<p class=\"import-Normal\">How do we know if an algorithm is unambiguous, correct, comes to an end, is general AND is at the right level of detail? We must test the algorithm. <em class=\"import-Emphasis\">Testing<\/em> means verifying that the algorithm does what we expect it to do. In our \u2018bake a cake\u2019 example we know our algorithm is \u2018working\u2019 if, in the end, we get something that looks, smells and tastes like a cake.<\/p>\n<h1>Verifying your Algorithm<\/h1>\n<div id=\"attachment_282\" style=\"width: 415px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-282\" class=\"wp-image-282\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232242\/keyboard-597007_1920.jpg\" alt=\"\" width=\"405\" height=\"273\" \/><\/p>\n<p id=\"caption-attachment-282\" class=\"wp-caption-text\">Figure 3 &#8220;<a href=\"https:\/\/pixabay.com\/en\/keyboard-computer-facebook-blue-597007\/\">Keyboard<\/a>&#8221; by <a href=\"https:\/\/pixabay.com\/en\/users\/geralt-9301\/\">Geralt<\/a> is licensed under <a href=\"https:\/\/creativecommons.org\/licenses\/by\/2.0\/\">CC 2<\/a><\/p>\n<\/div>\n<p class=\"import-Normal\">Your first step should be to carefully read through EACH step of the algorithm to check for ambiguity and if there is any information missing. To ensure that the algorithm is correct, terminates and is general for any input we devise \u2018test cases\u2019 for the algorithm.<\/p>\n<p class=\"import-Normal\">A test case is a set of inputs, conditions, and expected results developed for a particular computational problem to be solved. A test case is really just a question that you ask of the algorithm (e.g. if my list is the three numbers 2, 14, and 11 does the algorithm return the number 14?). The point of executing the test is to make sure the algorithm is correct, that it terminates and is general for any input.<\/p>\n<p class=\"import-Normal\">Good (effective) test cases:<\/p>\n<ul>\n<li>are easy to understand and execute<\/li>\n<li>are created with the user in mind (what input mistakes will be made? what are the preconditions?)<\/li>\n<li>make no assumptions (you already know what it is supposed to do)<\/li>\n<li>consider the boundaries for a specified range of values.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Let us look at the example algorithm from the previous section. The input for the algorithm is \u2018a list of positive numbers\u2019. To make it easy to understand and execute keep the test lists short. The preconditions are that the list only contains numbers and these numbers must be positive so include a test with a \u2018non-number\u2019 (i.e. a special character or a letter) and a test with a negative number. The boundaries for the list are zero and the highest positive number so include a test with zero and a large positive number. That is it! Here is an example of three different test cases.<\/p>\n<div style=\"margin: auto\">\n<table style=\"height: 244px;width: 661px\">\n<tbody>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 137.433px\">\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 283.65px\">\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 194.05px\">\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 39px;width: 137.433px\">\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 39px;width: 283.65px\">\n<p class=\"import-Normal\">List: 44, 14, 0, 1521, 89, 477<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 39px;width: 194.05px\">\n<p class=\"import-Normal\">1521<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 137.433px\">\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 283.65px\">\n<p class=\"import-Normal\">List: 18, 4, 72, *, 31<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 55px;width: 194.05px\">\n<p class=\"import-Normal\">Error (or no result)<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 10px\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 10px;width: 137.433px\">\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 10px;width: 283.65px\">\n<p class=\"import-Normal\">List: 22, -9, 52<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 10px;width: 194.05px\">\n<p class=\"import-Normal\">Error (or no result)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p class=\"import-Normal\">Manually, you should step through your algorithm using each of the three test cases, making sure that the algorithm does indeed terminate and that you get your expected result. As our algorithms and programs become more complex, skilled programmers often break each test case into individual steps of the algorithm\/program and indicate what the expected result of each step should be. When you write a detailed test case, you don\u2019t necessarily need to specify the expected result for each test step if the result is obvious.<\/p>\n<p class=\"import-Normal\">In computer programming we accept a problem to solve and develop an algorithm that can serve as a general solution. Once we have such a solution, we can use our computer to automate the execution. Programming is a skill that allows a competent programmer to take an algorithm and represent it in a notation (a program) that can be followed by a computer. These programs are written in <strong>programming languages <\/strong>(such as Python). Writing a correct and valid algorithm to solve a computational problem is key to writing good code. Learn to <em>Think First<\/em> and coding will come naturally!<\/p>\n<h1>The Process of Computational Problem Solving<\/h1>\n<p class=\"import-Normal\">Computational problem solving does not simply involve the act of computer programming. It is a process, with programming being only one of the steps. Before a program is written, a design for the program must be developed (the algorithm). And before a design can be developed, the problem to be solved must be well understood. Once written, the program must be thoroughly tested. These steps are outlined in Figure 5.<\/p>\n<div style=\"width: 587px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232245\/image6.png\" alt=\"image\" width=\"577\" height=\"567\" \/><\/p>\n<p class=\"wp-caption-text\">Figure 5: Process of Computational Problem Solving<span class=\"import-FootnoteReference\">[footnote]Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, pp17-18.[\/footnote]<\/span><\/p>\n<\/div>\n<h1>Values and Variables<\/h1>\n<p class=\"import-Normal\">A value is one of the basic things computer programs works with, like a password or a number of errors.<\/p>\n<p class=\"import-Normal\">Values belong to different types: 21 is an integer (like the number of errors), and \u2018comp15\u2019 is a string of characters (like the password). Python lets you give names to <em class=\"import-Emphasis\">values<\/em> <em class=\"import-Emphasis\">giving us the ability to generalize our algorithms.<\/em><\/p>\n<p class=\"import-Normal\">One of the most powerful features of a programming language is the ability to use variables. A variable is simply a name that refers to a value as shown below,<\/p>\n<table style=\"border-collapse: collapse;width: 100%\">\n<tbody>\n<tr>\n<td style=\"width: 50%\"><code>errors = 21<\/code><\/td>\n<td style=\"width: 50%\">variable <code>errors<\/code> is assigned the value 21<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%\"><code>password = \u2018comp15\u2019<\/code><\/td>\n<td style=\"width: 50%\">\u00a0variable <code>password<\/code> is assigned the value \u2018comp15\u2019<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Whenever the variable errors appears in a calculation the current value of the variable is used.<\/p>\n<table style=\"border-collapse: collapse;width: 100%\">\n<tbody>\n<tr>\n<td style=\"width: 50%\"><code>errors = 21<\/code><\/td>\n<td style=\"width: 50%\">variable <code>errors<\/code> is assigned the value 21<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%\"><code>errors = errors + 1<\/code><\/td>\n<td style=\"width: 50%\">variable <code>errors<\/code> is assigned the value of 21+1 (22)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"import-Normal\">We need some way of storing information (i.e. the number of errors or the password) and manipulate them as well. This is where variables come into the picture. Variables are exactly what the name implies &#8211; their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer&#8217;s memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names.<\/p>\n<p class=\"import-Normal\">Programmers generally choose names for their variables that are meaningful and document what the variable is used for. It is a good idea to begin variable names with a lowercase letter . The underscore character (_) can appear in a name and is often used in names with multiple words.<\/p>\n<p class=\"import-Normal\">A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of mathematical equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or something graphical, like processing user input on an ATM device.<\/p>\n<h1>What is a Program?<\/h1>\n<div style=\"width: 330px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232247\/image7.jpg\" alt=\"image\" width=\"320\" height=\"240\" \/><\/p>\n<p class=\"wp-caption-text\">Figure 6: \u201c<a href=\"https:\/\/flic.kr\/p\/86bi4j\">Python Code<\/a>\u201d by <a href=\"https:\/\/www.flickr.com\/photos\/nyuhuhuu\/\">nyuhuhuu<\/a> is licensed under <a href=\"https:\/\/creativecommons.org\/licenses\/by\/2.0\/\">CC-BY 2.0<\/a><\/p>\n<\/div>\n<p class=\"import-Normal\">The details look different in different computer programming languages, but there are some low-level conceptual patterns (constructs) that we use to write all programs. These constructs are not just for Python programs, they are a part of every programming language.<\/p>\n<p class=\"import-Normal\"><strong>input<\/strong> Get data from the \u201coutside world\u201d. This might be reading data from a file, or even some kind of sensor like a microphone or GPS. In our initial algorithms and programs, our input will come from the user typing data on the keyboard.<\/p>\n<p class=\"import-Normal\"><strong>output<\/strong> Display the results of the program on a screen or store them in a file or perhaps write them to a device like a speaker to play music or speak text.<\/p>\n<p class=\"import-Normal\"><strong>sequential<\/strong> <strong>execution<\/strong> Perform statements one after another in the order they are encountered in the script.<\/p>\n<p class=\"import-Normal\"><strong>conditional<\/strong> <strong>execution<\/strong> Checks for certain conditions and then executes or skips a sequence of statements.<\/p>\n<p class=\"import-Normal\"><strong>repeated<\/strong> <strong>execution<\/strong> Perform some set of statements repeatedly, usually with some variation.<\/p>\n<p class=\"import-Normal\"><strong>reuse<\/strong> Write a set of instructions once and give them a name and then reuse those instructions as needed throughout your program.<\/p>\n<p class=\"import-Normal\">Believe it or not, that\u2019s pretty much all there is to it. Every computer application you\u2019ve ever used, no matter how complicated, is made up of constructs that look pretty much like these. So you can think of programming as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of these basic constructs. The \u201cart\u201d of writing a program is composing and weaving these basic elements together many times over to produce something that is useful to its users.<\/p>\n<h1>Computational Problem\u00a0Design Using the Basic Programming Constructs<\/h1>\n<p class=\"import-Normal\">The key to better algorithm design and thus to programming lies in limiting the <strong>control structure<\/strong> to only three constructs as shown below.<\/p>\n<ol>\n<li>The Sequence structure (sequential execution)<\/li>\n<li>The Decision, Selection or Control structure (conditional execution)<\/li>\n<li>Repetition or Iteration Structure (repeated execution)<\/li>\n<\/ol>\n<div style=\"width: 722px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232249\/image8.png\" alt=\"image\" width=\"712\" height=\"401\" \/><\/p>\n<p class=\"wp-caption-text\">Figure 7: the 3 Programming Constructs<\/p>\n<\/div>\n<p><small>\u00a0<\/small>Let us look at some examples for the sequential control and the selection control.<\/p>\n<h3><a name=\"_Toc529437417\" id=\"_Toc529437417\"><\/a>Sequential Control Example<\/h3>\n<p class=\"import-Normal\">The following algorithm is an example of <strong>sequential control<\/strong>.<\/p>\n<div class=\"textbox\">\n<p><strong>Problem<\/strong>: Given two numbers, return the sum and the product of the two numbers.<\/p>\n<p><strong>Inputs<\/strong>: Two numbers.<\/p>\n<p><strong>Outputs<\/strong>: The sum and the product.<\/p>\n<ol>\n<li class=\"import-Normal\"><code>Start<\/code><\/li>\n<li class=\"import-Normal\"><code>display \u201cInput two numbers\u201d<\/code><\/li>\n<li style=\"list-style-type: none\"><\/li>\n<li class=\"import-Normal\"><code>sum = number1 + number2<\/code><\/li>\n<li class=\"import-Normal\"><code>print \u201cThe sum is \u201c, sum<\/code><\/li>\n<li class=\"import-Normal\"><code>product = number1 * number2<\/code><\/li>\n<li class=\"import-Normal\"><code>print \u201cThe product is \u201c, product<\/code><\/li>\n<li class=\"import-Normal\"><code>End<\/code><\/li>\n<\/ol>\n<\/div>\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\n<ul>\n<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\n<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\n<li><em>Is it guaranteed to terminate?<\/em> Yes. Sequential control, by its nature, always ends.<\/li>\n<li><em>Is it general for any input?<\/em> Yes. Any two numbers work in this design.<\/li>\n<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Here is an example of three different test cases that are used to verify the algorithm.<\/p>\n<div style=\"margin: auto\">\n<table style=\"height: 330px;width: 702px\">\n<tbody>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 167.35px;height: 57px\">\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 256.3px;height: 57px\">\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 232.483px;height: 57px\">\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 167.35px;height: 82px\">\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 256.3px;height: 82px\">\n<p class=\"import-Normal\">numbers 0 and 859<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 232.483px;height: 82px\">\n<p class=\"import-Normal\">sum is 859<br style=\"clear: both\" \/>product is 0<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 167.35px;height: 79px\">\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 256.3px;height: 79px\">\n<p class=\"import-Normal\">numbers -5 and 10<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 232.483px;height: 79px\">\n<p class=\"import-Normal\">sum is 5<br style=\"clear: both\" \/>product is -50<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 167.35px;height: 47px\">\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 256.3px;height: 47px\">\n<p class=\"import-Normal\">numbers 12 and 3<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 232.483px;height: 47px\">\n<p class=\"import-Normal\">sum is 15<br style=\"clear: both\" \/>product is 36<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>Selection Control<\/h3>\n<p class=\"import-Normal\">The following two algorithms are examples of <strong>selection<\/strong><strong> control<\/strong> which uses the \u2018IF\u2019 statement in most programming languages.<\/p>\n<div class=\"textbox\">\n<p class=\"import-Normal\"><strong>Problem<\/strong>: Given two numbers, the user chooses to either multiply, add or subtract the two numbers. Return the value of the chosen calculation.<\/p>\n<p class=\"import-Normal\"><strong>Inputs<\/strong>: Two numbers and calculation option.<\/p>\n<p class=\"import-Normal\"><strong>Outputs<\/strong>: The value of the chosen calculation.<\/p>\n<div class=\"textbox shaded\">\n<p>The relational (or comparison) operators used in selection control are:<\/p>\n<p>= is equal to<\/p>\n<p>&gt; is greater than<\/p>\n<p>&lt; is less than<\/p>\n<p>&gt;= is greater than or equal<\/p>\n<p>&lt;= is less than or equal<\/p>\n<p>&lt;&gt; is not equal to<\/p>\n<\/div>\n<ol>\n<li class=\"import-Normal\"><code>start<\/code><\/li>\n<li class=\"import-Normal\"><code>display \u201cchoose one of the following\u201d<\/code><\/li>\n<li class=\"import-Normal\"><code>display \u201cm for multiply\u201d<\/code><\/li>\n<li class=\"import-Normal\"><code>display \u201ca for add\u201d<\/code><\/li>\n<li class=\"import-Normal\"><code>display \u201cs for subtract\u201d<\/code><\/li>\n<li class=\"import-Normal\"><code>accept choice<\/code><\/li>\n<li class=\"import-Normal\"><code>display \u201cinput two numbers you want to use\u201d<\/code><\/li>\n<li class=\"import-Normal\"><code>accept number1, number2<\/code><\/li>\n<li class=\"import-Normal\"><code>if choice = m then answer= number1 * number2<\/code><\/li>\n<li class=\"import-Normal\"><code>if choice = a then answer= number1 + number2<\/code><\/li>\n<li class=\"import-Normal\"><code>if choice = s then answer= number1 -number212. if choice is not m, a, or s then answer is NONE<\/code><\/li>\n<li class=\"import-Normal\"><code>display answer<\/code><\/li>\n<li class=\"import-Normal\"><code>stop<\/code><\/li>\n<\/ol>\n<\/div>\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\n<ul>\n<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\n<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\n<li><em>Is it guaranteed to terminate?<\/em> Yes. The input is of finite length, so after accepting the user\u2019s choice and the two numbers the algorithm will stop.<\/li>\n<li><em>Is it general for any input?<\/em> Yes. Any two numbers work in this design and only a choice of a\u2019m\u2019, \u2018a\u2019, or \u2018s\u2019 will result in numeric output.<\/li>\n<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Here is an example of three different test cases that are used to verify the algorithm.<\/p>\n<div style=\"margin: auto\">\n<table style=\"height: 333px;width: 603px\">\n<tbody>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 136.067px;height: 73px\">\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 230.033px;height: 73px\">\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 191.033px;height: 73px\">\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 136.067px;height: 99px\">\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 230.033px;height: 99px\">\n<p class=\"import-Normal\">choice \u2018a\u2019<br style=\"clear: both\" \/>numbers -12 and 32<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 191.033px;height: 99px\">\n<p class=\"import-Normal\">answer is 20<br style=\"clear: both\" \/>terminate<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 136.067px;height: 99px\">\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 230.033px;height: 99px\">\n<p class=\"import-Normal\">choice \u2018s\u2019<br style=\"clear: both\" \/>numbers -2012 and 0<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 191.033px;height: 99px\">\n<p class=\"import-Normal\">answer is 2012<br style=\"clear: both\" \/>terminate<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 136.067px;height: 60px\">\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 230.033px;height: 60px\">\n<p class=\"import-Normal\">choice \u2018**\u2019<br style=\"clear: both\" \/>numbers 8 and 4<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 191.033px;height: 60px\">\n<p class=\"import-Normal\">answer is NONE<br style=\"clear: both\" \/>terminate<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p class=\"import-Normal\">This example uses an extension of the simple selection control structure we just saw and is referred to as the \u2018IF-ELSE\u2019 structure.<\/p>\n<div class=\"textbox\">\n<p>&nbsp;<\/p>\n<p><strong>Problem<\/strong>: Accept from the user a positive integer value representing a salary amount, return tax due based on the salary amount.<\/p>\n<p><strong>Inputs<\/strong>: One positive integer number.<\/p>\n<p><strong>Outputs<\/strong>: The calculated tax amount.<\/p>\n<div class=\"textbox shaded\">\n<p>The relational (or comparison) operators used in selection control are:<\/p>\n<p>= is equal to<\/p>\n<p>&gt; is greater than<\/p>\n<p>&lt; is less than<\/p>\n<p>&gt;= is greater than or equal<\/p>\n<p>&lt;= is less than or equal<\/p>\n<p>&lt;&gt; is not equal to<\/p>\n<\/div>\n<ol>\n<li><code>start<\/code><\/li>\n<li><code>accept salary<\/code><\/li>\n<li><code>If salary &lt; 50000 then<\/code><\/li>\n<li><code>Tax = 0 Else<\/code><\/li>\n<li><code>If salary &gt; 50000 AND salary &lt; 100000 then<\/code><\/li>\n<li><code>Tax = 50000 * 0.05 Else<\/code><\/li>\n<li><code>Tax = 100000 * 0.30<\/code><\/li>\n<li><code>End IF<\/code><\/li>\n<li><code>display Tax<\/code><\/li>\n<li><code>stop<\/code><\/li>\n<\/ol>\n<\/div>\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\n<ul>\n<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\n<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\n<li><em>Is it guaranteed to terminate?<\/em> Yes. The input is of finite length, so after accepting the user\u2019s number, even if it is negative, the algorithm will stop.<\/li>\n<li><em>Is it general for any input?<\/em> Yes. Any number entered in this design will work.<\/li>\n<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Here is an example of three different test cases that are used to verify the algorithm.<\/p>\n<div style=\"margin: auto\">\n<table style=\"height: 365px;width: 605px\">\n<tbody>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 150.283px\">\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 199.45px\">\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 209.4px\">\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 150.283px\">\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 199.45px\">\n<p class=\"import-Normal\">salary of 0<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 209.4px\">\n<p class=\"import-Normal\">tax is 0<br style=\"clear: both\" \/>terminate<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 150.283px\">\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 199.45px\">\n<p class=\"import-Normal\">salary of 75000<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 209.4px\">\n<p class=\"import-Normal\">tax is 2500<br style=\"clear: both\" \/>terminate<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 150.283px\">\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 199.45px\">\n<p class=\"import-Normal\">salary of 120000<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 209.4px\">\n<p class=\"import-Normal\">tax is 30000<br style=\"clear: both\" \/>terminate<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>Iterative Control Examples<\/h3>\n<p class=\"import-Normal\">The third programming control is the iterative or, also referred to as, the repetition structure. This control structure causes certain steps to be repeated in a sequence a specified number of times or until a condition is met. This is what is called a \u2018loop\u2019 in programming<\/p>\n<p class=\"import-Normal\">In all programming languages there are generally two options: an indefinite loop (the Python \u2018WHILE\u2019 programming statement) and a definite loop (the Python \u2018FOR\u2019 programming statement). We can use these two constructs, WHILE and FOR, for iterations or loops in our algorithms.<\/p>\n<div class=\"textbox\">\n<p><strong>Note for Reader: <\/strong><br \/>\nA <strong>definite loop<\/strong> is where we know exactly the number of times the loop\u2019s body will be executed. Definite iteration is usually best coded as a Python for loop. An <strong>indefinite loop<\/strong> is where we do not know before entering the body of the loop the exact number of iterations the loop will perform. The loop just keeps going until some condition is met. A while statement is used in this case.<\/p>\n<\/div>\n<p class=\"import-Normal\">The following algorithm is an example of <strong>iterative<\/strong><strong> control<\/strong> using<strong> WHILE<\/strong>.<\/p>\n<div class=\"textbox\">\n<p class=\"import-Normal\"><strong>Problem<\/strong>: Print each keyboard character the users types in until the user chooses the \u2018q\u2019 (for \u2018quit\u2019) character.<\/p>\n<p class=\"import-Normal\"><strong>Inputs<\/strong>: A series of individual characters.<\/p>\n<p class=\"import-Normal\"><strong>Outputs<\/strong>: Each character typed in by the user.<\/p>\n<ol>\n<li class=\"import-Normal\"><code>Start<\/code><\/li>\n<li class=\"import-Normal\"><code>initialize (set) letter = \u2018a\u2019<\/code><\/li>\n<li class=\"import-Normal\"><code>WHILE letter &lt;&gt; \u2018q\u2019<\/code><\/li>\n<li class=\"import-Normal\"><code>ACCEPT letter<\/code><\/li>\n<li class=\"import-Normal\"><code>DISPLAY \u201cThe character you typed is\u201d, letter<\/code><\/li>\n<li class=\"import-Normal\"><code>end WHILE<\/code><\/li>\n<li class=\"import-Normal\"><code>Stop<\/code><\/li>\n<\/ol>\n<\/div>\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\n<ul>\n<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\n<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\n<li><em>Is it guaranteed to terminate?<\/em> Yes. The input is of finite length, so after accepting the user\u2019s keyboard character, even if it is not a letter, the algorithm will stop.<\/li>\n<li><em>Is it general for any input?<\/em> Yes. Any keyboard character entered in this design will work.<\/li>\n<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Here is an example of three different test cases that are used to verify the algorithm.<\/p>\n<div style=\"margin: auto\">\n<table style=\"height: 373px;width: 632px\">\n<tbody>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 134.667px\">\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 144.083px\">\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 307.383px\">\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 134.667px\">\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 144.083px\">\n<p class=\"import-Normal\">letter \u2018z\u2019<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 307.383px\">\n<p class=\"import-Normal\">The character you typed is z. <br style=\"clear: both\" \/>Ask for another letter.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 134.667px\">\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 144.083px\">\n<p class=\"import-Normal\">letter \u20188\u2019<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 307.383px\">\n<p class=\"import-Normal\">The character you typed is 8<br style=\"clear: both\" \/>Ask for another letter.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 134.667px\">\n<p class=\"import-Normal\" style=\"text-align: center\">3<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 144.083px\">\n<p class=\"import-Normal\">letter \u2018q\u2019<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 307.383px\">\n<p class=\"import-Normal\">The character you typed is q. <br style=\"clear: both\" \/>Terminate.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p class=\"import-Normal\">The following algorithm is an example of <strong>iterative<\/strong><strong> control<\/strong> using<strong> FOR<\/strong>. This statement is used when the number of iterations is known in advance.<\/p>\n<div class=\"textbox\">\n<p class=\"import-Normal\"><strong>Problem<\/strong>: Ask the user how many words they want to enter then print the words entered by the user.<\/p>\n<p class=\"import-Normal\"><strong>Inputs<\/strong>: Number of words to be entered; this value must be a positive integer greater than zero. Individual words.<\/p>\n<p class=\"import-Normal\"><strong>Outputs<\/strong>: Each word typed in by the user.<\/p>\n<ol>\n<li class=\"import-Normal\">\n<pre>Start<\/pre>\n<\/li>\n<li class=\"import-Normal\">\n<pre>accept num_words (must be at least one)<\/pre>\n<\/li>\n<li class=\"import-Normal\">\n<pre>repeat num_words times (FOR 1 to num_words)<\/pre>\n<\/li>\n<li class=\"import-Normal\">\n<pre>accept word<\/pre>\n<\/li>\n<li class=\"import-Normal\">\n<pre>DISPLAY \u201cThe word you entered is\u201d, word<\/pre>\n<\/li>\n<li class=\"import-Normal\">\n<pre>end FOR<\/pre>\n<\/li>\n<li class=\"import-Normal\">\n<pre>Stop<\/pre>\n<\/li>\n<\/ol>\n<\/div>\n<p class=\"import-Normal\">Does this meet the criteria for being an algorithm?<\/p>\n<ul>\n<li><em>Is it unambiguous?<\/em> Yes. Each step of the algorithm consists of uncomplicated operations, and translating each step into programming code is straight forward.<\/li>\n<li><em>Does it have defined inputs and outputs?<\/em> Yes.<\/li>\n<li><em>Is it guaranteed to terminate?<\/em> Yes. The input is of finite length, so after accepting the user\u2019s number of words to enter and any characters typed on the keyboard, even if it is not a \u2018word\u2019 per say, the algorithm will stop.<\/li>\n<li><em>Is it general for any input?<\/em> Yes. Any positive integer greater than zero and any size \u2018word\u2019 will work.<\/li>\n<li><em>Does it produce the correct result?<\/em> Yes. When tested, the results are what are expected.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Here is an example of two different test cases that are used to verify the algorithm.<\/p>\n<div style=\"margin: auto\">\n<table style=\"height: 416px;width: 680px\">\n<tbody>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 135.517px\">\n<p class=\"import-Normal\"><strong>Test Case #<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 147.7px\">\n<p class=\"import-Normal\"><strong>Input Values<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 350.917px\">\n<p class=\"import-Normal\"><strong>Expected <\/strong><strong>R<\/strong><strong>esult<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 135.517px\">\n<p class=\"import-Normal\" style=\"text-align: center\">1<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 147.7px\">\n<p class=\"import-Normal\">num_words 1<br style=\"clear: both\" \/>word \u2018code\u2019<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 350.917px\">\n<p class=\"import-Normal\">The word you entered is \u2018code\u2019. <br style=\"clear: both\" \/>Terminate.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 14.4pt\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 135.517px\">\n<p class=\"import-Normal\" style=\"text-align: center\">2<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 147.7px\">\n<p class=\"import-Normal\">num_words 3<br style=\"clear: both\" \/>word \u2018coding\u2019<br style=\"clear: both\" \/><br style=\"clear: both\" \/>word \u2018is\u2019<br style=\"clear: both\" \/><br style=\"clear: both\" \/><br style=\"clear: both\" \/>word \u2018fun\u2019<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 350.917px\">\n<p class=\"import-Normal\">The word you entered is \u2018coding\u2019. <br style=\"clear: both\" \/>Ask for another word.<br style=\"clear: both\" \/><br style=\"clear: both\" \/>The word you entered is \u2018is\u2019. <br style=\"clear: both\" \/>Ask for another word.<br style=\"clear: both\" \/><br style=\"clear: both\" \/>The word you entered is \u2018fun\u2019. <br style=\"clear: both\" \/>Terminate.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"textbox\"><strong>Note to Reader:<br \/>\n<\/strong><br \/>\nAn <strong>infinite loop<\/strong> is a loop that executes its block of statements repeatedly until the user forces the program to quit. Once the program flow enters the loop\u2019s body it cannot escape. Infinite loops sometimes are by design. For example, a long-running server application like a Web server may need to continuously check for incoming connections. The Web server can perform this checking within a loop that runs indefinitely.Beginning programmers, unfortunately, all too often create infinite loops by accident, and these infinite loops represent logic errors in their programs<\/div>\n<h1>The Role of Programming in the Field of Informatics<\/h1>\n<div style=\"width: 527px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232252\/image9.jpg\" alt=\"image\" width=\"517\" height=\"216\" \/><\/p>\n<p class=\"wp-caption-text\">Figure8: iPhone apps by Jaap Arriens\/NurPhoto via Getty Images (abcnews.go.com)<\/p>\n<\/div>\n<p class=\"import-Normal\">You see computer programming in use every day. When you use Google or your smartphone, or watch a movie with special effects, there is programing at work. When you order a product over the Internet, there is code in the web site, in the cryptography used to keep your credit card number secure, and in the way that UPS routes their delivery vehicle to get your order to you as quickly as possible.<\/p>\n<p class=\"import-Normal\">Programming is indeed important to an informatics professional as they are interested in finding solutions for a wide variety of computational problems involving data.<\/p>\n<p class=\"import-Normal\">When you Google the words \u201cpie recipe,\u201d Google reports that it finds approximately 38 million pages, ranked in order of estimated relevance and usefulness. Facebook has approximately 1 billion active users who generate over 3 billion comments and \u201cLikes\u201d each day. GenBank, a national database of DNA sequences used by biologists and medical researchers studying genetic diseases, has over 100 million genetic sequences with over 100 billion DNA base pairs. According to the International Data Corporation, by 2020 the digital universe \u2013 the data we create and copy annually \u2013 will reach 44 zettabytes, or 44 trillion gigabytes.<\/p>\n<div style=\"width: 605px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232255\/image10.jpg\" alt=\"image\" width=\"595\" height=\"277\" \/><\/p>\n<p class=\"wp-caption-text\">Figure 9: The Digital Universe (<a href=\"http:\/\/www.emc.com\/leadership\/digital-universe\/2014iview\/images\">www.emc.com\/leadership\/digital-universe\/2014iview\/images<\/a>)<\/p>\n<\/div>\n<p><small>\u00a0<\/small>Doing meaningful things with data is challenging, even if we\u2019re not dealing with millions or billions of things. In this book, we will be working with smaller sets of data. But much of what we\u2019ll do will be applicable to very large amounts of data too.<\/p>\n<h1>Unit Summary<\/h1>\n<p class=\"import-Normal\" style=\"margin-left: 18pt\">Computational Thinking is the thought processes involved in formulating a problem and expressing its solution in a way that a computer\u2014human or machine\u2014can effectively carry out.<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 18pt\">Computational Thinking is what comes before any computing technology\u2014thought of by a human, knowing full well the power of automation.<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 18pt\">Writing a correct and valid algorithm to solve a computational problem is key to writing good code.<\/p>\n<ol>\n<li class=\"import-Normal\">First, understand the problem\n<ol type=\"a\">\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">What are the inputs?<\/em><\/li>\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">What are the outputs (or results)?<\/em><\/li>\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Can we break the problem into parts?<\/em><\/li>\n<\/ol>\n<\/li>\n<li class=\"import-Normal\">Second, design the process (write the <strong style=\"text-align: initial;font-size: 1em\">algorithm<\/strong><span style=\"text-align: initial;font-size: 1em\">!).<\/span>\n<ol type=\"a\">\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Think about the connections between the input &amp; output.<\/em><\/li>\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Consider designing \u2018backwards\u2019.<\/em><\/li>\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Have you seen the problem before? In a slightly different form?<\/em><\/li>\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Can you solve part of the problem?<\/em><\/li>\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Did you use all the inputs?<\/em><\/li>\n<\/ol>\n<\/li>\n<li class=\"import-Normal\">Third, test your design.\n<ol type=\"a\">\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Can you test it on a variety of inputs?<\/em><\/li>\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Can you think of how you might write the algorithm differently if you had to start again?<\/em><\/li>\n<\/ol>\n<\/li>\n<li class=\"import-Normal\">Last, write the program (we will be addressing this in our next Unit).\n<ol type=\"a\">\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Does it solve the problem? Does it meet all the requirements? Is the output correct?<\/em><\/li>\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Does it terminate?<\/em><\/li>\n<li class=\"import-Normal\"><em style=\"text-align: initial;font-size: 1em\">Is it general for all cases?<\/em><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h1>Practice Problems<\/h1>\n<ol>\n<li>Write about a process in your life (e.g. driving to the mall, walking to class, etc.) and estimate the number of steps necessary to complete the task. Would you consider this a complex or simple task? What happens if you scale that task (e.g. driving two states away to the mall)? Is your method the most efficient? Can you come up with a more efficient way?<\/li>\n<li>Given five cards randomly placed in a row like the ones below write an algorithm to sort the cards. How would your algorithm change if there were 10 cards? What about 100 cards?<br \/>\n<img decoding=\"async\" class=\"aligncenter\" style=\"text-align: center;font-size: 1em\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232257\/image11.png\" alt=\"image\" width=\"301.333333333333px\" height=\"64.8666666666667px\" \/><\/li>\n<li>Write an algorithm to find the average of 25 test grades out of a possible 100 points.<\/li>\n<li>If you are given three sticks, you may or may not be able to arrange them in a triangle. For example, if one of the sticks is 12 inches long and the other two are one inch long, it is clear that you will not be able to get the short sticks to meet in the middle. For any three lengths, there is a simple test to see if it is possible to form a triangle: \u201cIf any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can.\u201dWrite an algorithm that accepts three integers as arguments, and that displays either \u201cYes\u201d or \u201cNo,\u201d depending on whether you can or cannot form a triangle from sticks with the given lengths.<\/li>\n<li>ROT13 is a weak form of encryption that involves \u201crotating\u201d each letter in a word by 13 places. To rotate a letter means to shift it through the alphabet, wrapping around to the beginning if necessary, so \u2018A\u2019 shifted by 3 is \u2018D\u2019 and \u2018Z\u2019 shifted by 1 is \u2018A\u2019. <br style=\"clear: both\" \/>Write an algorithm that accepts a word and an integer from the user, and that prints a new encrypted word that contains the letters from the original word \u201crotated\u201d by the given amount (the integer input). For example, \u201ccheer\u201d rotated by 7 is \u201cjolly\u201d and \u201cmelon\u201d rotated by \u221210 is \u201ccubed.\u201d<\/li>\n<li>Write an algorithm to accept a score between 0.0 and 1.0. If the score is out of range, display an error message. If the score is between 0.0 and 1.0, display a grade using the following table:<br \/>\n<table style=\"border-collapse: collapse;width: 100%\">\n<tbody>\n<tr>\n<td style=\"width: 1.43062%\"><strong>Score<\/strong><\/td>\n<td style=\"width: 1.43062%\"><strong>Grade<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 1.43062%\">&gt;= 0.9<\/td>\n<td style=\"width: 1.43062%\">A<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 1.43062%\">&gt;= 0.8<\/td>\n<td style=\"width: 1.43062%\">B<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 1.43062%\">&gt;= 0.7<\/td>\n<td style=\"width: 1.43062%\">C<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 1.43062%\">&gt;= 0.6<\/td>\n<td style=\"width: 1.43062%\">D<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 1.43062%\">&lt; 0.6<\/td>\n<td style=\"width: 1.43062%\">E<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/li>\n<li>Write an algorithm which repeatedly accepts numbers until the user enters \u201cdone\u201d. Once \u201cdone\u201d is entered, display the total sum of all the numbers, the count of numbers entered, and the average of all the numbers.<\/li>\n<li>Write an algorithm that sums a series of ten positive integers entered by the user excluding all numbers greater than 100. Display the final sum.<\/li>\n<\/ol>\n<hr class=\"before-footnotes clear\" \/><div class=\"footnotes\"><ol><li id=\"footnote-35-1\">Wing, Jeannette M. \"Computational thinking.\" Communications of the ACM 49.3 (2006): 33-35. <a href=\"#return-footnote-35-1\" class=\"return-footnote\" aria-label=\"Return to footnote 1\">&crarr;<\/a><\/li><li id=\"footnote-35-2\">Wing, Jeannette M. \"Computational thinking.\" Communications of the ACM 49.3 (2006): 33-35. <a href=\"#return-footnote-35-2\" class=\"return-footnote\" aria-label=\"Return to footnote 2\">&crarr;<\/a><\/li><\/ol><\/div>","protected":false},"author":23485,"menu_order":1,"template":"","meta":{"_candela_citation":"[]","CANDELA_OUTCOMES_GUID":"","pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[48],"contributor":[],"license":[],"class_list":["post-35","chapter","type-chapter","status-publish","hentry","chapter-type-numberless"],"part":3,"_links":{"self":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/users\/23485"}],"version-history":[{"count":4,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters\/35\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters\/35\/revisions\/110"}],"part":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters\/35\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapter-type?post=35"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/contributor?post=35"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/license?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}