Before you begin learning how to program, it is important to note that the process of programming has been around since long before the advent of computers. Underlying programming is the design and implementation of algorithms. An algorithm is a clear, step-by-step sequence of instructions for performing some task.
Whether you recognize them or not, you are confronted with algorithms every day. When you shower in the morning, an algorithm for shampooing your hair is printed right on the bottle:
Specify an algorithm for identifying the oldest person in a room full of
people. That is, give a sequence of simple instructions that a person could
follow to identify the oldest person in a room. Note that appearances can be
deceiving, so don't rely on instructions such as "look for the person with the
most gray in their hair." You may assume that if you ask a person their age,
they will tell you accurately.
Suppose you have two glasses, a red glass containing Root Beer and a
blue glass containing Mountain Dew. Specify an algorithm for swapping the
contents of the two glasses. That is, when done executing your
instructions, the red glass should contain Mountain Dew while the blue glass
should contain Root Beer. You may assume the existence of an additional white
glass if you wish.
A key component in the concept of an algorithm is the person or entity that is to carry out the prescribed steps. Similar to the old adage about "a tree falling in the woods", an algorithm is meaningless without someone or something to execute it. And in order for the algorithm to be well-defined, its steps must be clear and unambiguous to that intended executor. Look again at the algorithm for shampooing your hair. Do you find each of the steps to be clear? Chances are, the typical adult would have no problem understanding what "rinse thoroughly" and "repeat if necessary" mean. With a lifetime of experience washing their hair, these phrases have a common sense meaning. Thus, this may be viewed as a well-defined algorithm for the typical adult.
Now consider the shampooing algorithm from the perspective of a small child. Would a child who had never washed their own hair be able to tell the difference between thorough and non-thorough rinsing? Would they be able to determine whether repetition were necessary? Probably not. For that executor, then, this is not a well defined algorithm. To a small child, the individual steps are to vague to describe a precise sequence of actions.
Specify an algorithm for traveling from your dorm room to the computer lab
in South College. Assume that the person who will follow your instructions is a
typical Dickinson freshman, with knowledge of the general layout of the campus
and the location of landmark buildings such as Old West and the HUB.
Now specify an algorithm for traveling from your dorm room to the computer
lab, with the intended audience being someone completely unfamiliar with
Dickinson. Note the level of detail required here as opposed to in the previous
The above exercises were designed to pound home the point that algorithms are defined with specific executor in mind. When you supplied directions in Exercise 3, you were able to rely on the background knowledge of the student following those directions. In contrast to the directions from Exercise 4, this made for shorter, more high-level instructions. Without a familiarity with the campus, the directions had to be more detailed and low-level.
Describing algorithms in a spoken language such as English (or French, Spanish, German, ...) certainly opens up possibilities for misunderstandings. Different people may read the same sentence and yet have different interpretations of its meaning. Phrases such as "the ugly building" and "take a hard left" have very subjective meanings. Fortunately, spoken languages are rich enough and usually contain enough redundancy to avoid such problems. Still, who among us has not gotten lost after faithfully following someone's directions?
Consider the task of averaging a list of numbers. An English description of an algorithm might be as follows:
Cut-and-paste the averaging code from above into the interpreter and
execute it. Report the output.
re-executed in order to perform different computations. Edit the code by
adding the number 100 at the end of the list, preceded by a comma to separate it from the previous number. Then execute this modified code
to obtain the average of these five numbers. Report the output.
For the most part, human beings are able to look past typographical or grammatical errors in language and discern the desired meaning. Computers, on the other hand, are completely inflexible with respect to errors. Even the slightest variation from the specified form will cause execution to be halted. For example, if you forgot the closing right parenthesis in line 4 of the code above, execution would fail and a window would appear with the following error message:
Typographical errors such as this are known in computer terms as syntax errors since they are violations of the strict syntax of the language, i.e., the rules which specify the form of statements. In order to assist you in fixing syntax errors, error messages attempt to identify the cause and location of each error. In this case, the error message specifies that there is a missing right parenthesis and points to the location where it should be placed. Note that a line number for the error is also specified, although the line number reported is always one too large (this is due to technical reasons that we will not discuss).
Simulate a syntax error by misspelling one of the words in the program. For example, in line 8 (starting with average), change the spelling of "sum" to "some". What is the result? Is the error message useful in locating the actual error?