Ingredients
• 1 cup melted butter • 2 cups brown sugar • 2 eggs
• 3 cups flour
• 1 teaspoon baking powder • 1 teaspoon baking soda • 2 cups chocolate chips
Directions
1. Preheat the oven to 375 degrees F. 2. Line a cookie sheet with parchment paper
3. In a bowl, stir together the butter, brown sugar and eggs.
4. In a separate bowl, combine the flour, baking powder and baking soda. Gradually combine with the sugar mixture.
5. Add the chocolate chips
6. Fill the cookie sheet with one-spoonful drops of the cookie dough. 7. Bake dough for 9 minutes
8. Cool for five minutes before removing from cookie sheet.
Image courtesy of http://www.flickr.com/photos/amagill/34754258/
In the field of computer science, an algorithm is any well-defined sequence of actions that takes a set of values as input and produces some set of values as output. In other words, an algorithm is a sequence of com- putational actions that transform the input into the desired output. A
computational understanding of the chocolate chip cookie recipe views the list of ingredients as the input to the recipe; the directions constitute the algorithm. The directions describe an orderly sequence of discrete actions that transform the input into the desired output. The output is, of course, a plate full of the best chocolate chip cookies in the world.
Any useful algorithm must ensure that several important properties are maintained. One of the most important requirements is that each of the individual actions referred to in the algorithm be meaningful. When computer programmers write an algorithm, programmers must know the precise meaning of each action they write; just as a chefs must understand the meaning of each action described in a recipe. Computer programmers refer to the meaning of an action as the semantics of an action. The phrase semantics refers to the meaning of the actions that occur in an algorithm.
Another requirement is that the actions of an algorithm must have only one possible interpretation, so that there is no misunderstanding about the semantics of the action. We say that an action must be unambigu- ous; meaning that the action is not subject to conflicting interpretations. In the recipe example, each step is written in such a way that a chef can easily understand and therefore follow the instructions. Actions like pre- heat, line, stir, combine, add, bake, and cool are common terms that all chefs fully understand and are not subject to multiple interpretations. If we replace the word bake in step 7 with the term heat, the directions now say to “Heat dough for 9 minutes.” This makes the algorithm less clear and therefore more difficult to follow since chefs do not typically use this term to describe baking cookies. An inexperienced cook might consider broil- ing the dough or simply letting it set out at room temperature in order to heat the dough.
An algorithm must also ensure that the order in which the actions occur be well defined. It should be self-evident that the order of actions taken when making chocolate chip cookies is vitally important. If, for example, we swap steps 6 and 7 in the cookie recipe, we end up placing a bowl full of dough into the oven and baking one giant cookie that we remove from the oven and carve up into small spoon-sized chunks that we carefully drop onto the cookie sheet after which the dough is allowed to bake for nine minutes.
Finally we note that the number of actions described by an algorithm must be finite rather than infinite. No goal can be reached if we are required to follow a never-ending sequence of actions in order to achieve
the goal. A computer scientist might express this requirement by saying that an algorithm must halt in order to be useful.
5.2 SOFTWARE AND PROGRAMMING LANGUAGES
A computer is able to perform a surprisingly small number of relatively simple actions such as adding two integer numbers or checking to see if two integer numbers are equal. Of course, computers can solve tremen- dously complex problems and achieve incredibly useful outcomes if they perform these actions quickly enough and in the right sequence.
Computer software, also referred to as a program, provides the instruc- tions for telling a computer the algorithm that it should follow to achieve some goal. Software cannot be expressed in human languages such as English or Chinese or Spanish since these languages are not easily used to describe computation with the necessary technical precision. Computer software is therefore expressed in a programming language. A program- ming language is a language that is designed to precisely and compactly express computational algorithms. Programming languages must there- fore be able to represent the input and output data of an algorithm along with the sequence of actions that a computer must follow to transform the input into the desired output. When a computer executes a computer pro- gram, the computer simply follows the steps that are given by the program in order to obtain the desired output from the provided input.
Just like there are numerous human languages in the world, there are a surprisingly large number of different programming languages in com- mon use today. Although each programming language must be able to precisely express a computational algorithm, not all programming lan- guages express computation in the same way. Many programming lan- guages express computation as a sequence of commands that are issued to the computer. These commands are often referred to as imperatives and languages that express computation in this fashion are therefore known as imperative languages. Other languages express computation using very different paradigms. Functional languages emphasize the relationship between input and output as an intrinsic entity rather than viewing com- putation as a sequence of individual actions that move from input to out- put. Declarative languages emphasize the expression of facts from which output can be generated by applying relevant facts to the input. Other paradigms include logical programming, object-oriented programming, and concurrent programming.
In this chapter we will use an informal imperative language for express- ing computational algorithm. Although a computer cannot directly exe- cute this informal language, the programs that we will describe can be easily translated into a real programming language.
5.3 ACTIONS
As we have already mentioned, computational algorithms must be expressed as a well-defined sequence of actions that the computer must follow. Perhaps the most critical aspect of computation involves know- ing what actions a computer can take and also knowing what actions a computer cannot take! Consider, for example, the algorithm shown in Figure 5.1 that gives actions allowing us to travel from New York City to Los Angeles without consuming any fuel or electrical energy. Although the algorithm is very simple to write and very simple to understand, it is not useful since it requires us to perform an impossible action. The action requiring a person to jump 2,440 miles is of course impossible to perform and this renders the algorithm meaningless.
Since any executable algorithm must be expressed as a sequence of pos- sible actions, we must first understand what actions a computer might pos- sibly take. Such actions are sometimes referred to as computable actions or, more formally, as computable functions. The remainder of this section informally defines a small but powerful set of computable functions that allow us to express a surprisingly large number of very useful algorithms. 5.3.1 Name Binding
One of the simplest, yet also one of the most important, actions that a computer can perform is that of naming the elements of a program. In