Addition--Computer Problem Solving [ Understanding Your System ] MPE/iX 5.0 Documentation
Understanding Your System
Addition--Computer Problem Solving
You may recall this small problem in addition from Chapter 2 .
234
19
8
611
----
872
[REV BEG]
Computers solve such problems by following the instructions designed into
them. The instructions are written in one of the many programming
languages that are available.
________________________________________________________________________
|There is no need to learn a programming language, unless the subject |
|catches your interest or your job requires it. Indeed, you may wish |
|to skip over the following example and continue your reading with the |
|next chapter. You will lose nothing by doing so. The value of the |
|following example lies in shedding a little light on how computers do |
|what they do. |
________________________________________________________________________
Here is one computer solution to the task of adding numbers. This
program will take numbers from the keyboard, one after the other, and
continue adding them together, one after the other, until you enter a
zero (0). Then it will display the sum of the numbers that you entered.
The instructions are written in a programming language called Pascal:[REV
END]
PROGRAM ADD(INPUT, OUTPUT); {line 1}
VAR DIGITS, TOTAL : REAL; {line 2}[REV BEG]
BEGIN {line 3}
TOTAL := 0; {line 4}
readln(DIGITS); {line 5}
while DIGITS <> 0 do {line 6}
begin {line 7}
TOTAL := TOTAL + DIGITS; {line 8}
readln(DIGITS); {line 9}
end; {line 10}
writeln(TOTAL : 10 : 2); {line 11}
END. {line 12}
Unless you have encountered programming, it looks odd. If you have any
acquaintance with programming, you will recognize that it is simple,
unsophisticated, perhaps a bit primitive. It is not the only possible
method--nor is it necessarily the best--but it accomplishes the task of
adding together numbers. Someone else might design a different method,
using different steps. Someone else might use a different programming
language--perhaps the language called C, or COBOL, or FORTRAN, or yet
another language.
Whatever the language, whatever the method, each method would be
considered an algorithm, in this case, an algorithm for adding numbers.
Algorithms, like the rules that you use for adding numbers, are sets of
instructions--recipes, if you like--for solving a problem or
accomplishing some task.
The algorithm described here does not tell us what the sum of the numbers
might be. Rather, it describes, in minute detail, the steps that are to
be followed in adding numbers together to produce a sum.
Each programming language has its own methods, its own "grammar," its own
"punctuation," and its own vocabulary.
If you do nothing else, make note that the instructions in this program
are very specific.
1. PROGRAM ADD(INPUT, OUTPUT); {line 1}
This program statement is an introduction. It tells the computer
"this is a program called ADD, and it will accept input from the
user and produce output."
In the vocabulary of Pascal, it tells the computer quite
specifically that information will come from the keyboard (INPUT,
which happens to be the file $STDIN during a session). And it
tells the computer that information will be returned to the video
screen (OUTPUT, which happens to be $STDLIST during a session).
Now the computer knows which of its parts (in addition to the
processor) will be active.
A different introductory statement could be used to tell the
computer that information will come from another source or go to
another destination.
2. VAR DIGITS, TOTAL : REAL; {line 2}
This is another introductory statement. It tells the computer
"create two variables (places to hold something in memory) and
make them big enough to hold numbers with fractions." Numbers
without fractions take up less room in memory than do numbers with
fractions.
VAR means "variable." In the vocabulary of Pascal, it tells the
computer that the words DIGITS and TOTAL will be used to hold
something.
REAL tells the computer that what will be held in DIGITS and TOTAL
will be numbers and, more precisely, that these numbers might
include fractional parts (263.6, for instance).
For as long as the program is active, the computer understands
that DIGITS and TOTAL really stand for numbers. Numbers without
fractional parts are called integers.
This might seem unnecessary. Computers work with numbers, do they
not? But this and every other statement is vital to the program.
From this statement, the computer knows that whatever is put into
the variables DIGITS and TOTAL must be numbers (and only numbers),
not letters or words. It knows, too, that it must create in its
electronic memory two distinct places to hold numbers.[REV END]
3. BEGIN {line 3}
"Now, after all this introductory material, here is what you (the
program) will do." This statement signals the end of the
introductory material and the beginning of the main body of the
instructions.
4. TOTAL := 0; {line 4}
[REV BEG]
"Begin with a sum of zero." The computer must be told explicitly
where to start. Where it starts is by putting the value 0 (zero)
into the area of memory represented by the variable TOTAL. Without
this statement, there is no way to predict what the starting value
of TOTAL might be, and that could cause problems. (As yet, the
area of memory represented by the variable DIGITS holds no useful
value.)[REV END]
5. readln(DIGITS); {line 5}
"Take a number from the keyboard." Implicit in this statement is
the instruction to "remember" the number that comes from the
keyboard by putting it into the memory area called DIGITS.
6. while DIGITS <> 0 do {line 6}
"Examine the number taken from the keyboard. If it is not equal
to zero, look for the begin following do and start there. If the
number is zero, proceed to the step following the next end that
you find." (The <> means "is not equal to" or "does not equal.")
There are a lot of instructions in this one, short statement.
Programming languages are precise, and they compress a lot of
meaning into very few words.
A very natural question at this point is, "Why not say 'if' if you
mean 'if'?"
The answer lies in the meaning of the "words" used in the Pascal
instructions. Pascal's while and if are called conditionals--they
are decision-makers. In its simplest use, Pascal's if means "make
this comparison, make a decision, then continue."
Pascal's while, on the other hand, means "for as long as some
condition is true...." And that is what we want the program to do:
we want to be able to enter many different numbers, over and over,
and have them added, one after another, to yield a sum. And we
want the computer to stop taking and adding numbers when we enter
a zero.
In the language of Pascal, while is a convenient way to start a
loop--a sequence of steps that is repeated, over and over, until
we give the computer some reason to cease the repetition. Each
time we enter a number, while says: "Is this zero? If it is not
zero, do whatever comes after the next begin. If it is zero, look
for the next end and pick up the instruction that follows."
7. begin {line 7}
"Start here and follow the next sequence of instructions until you
find an end; statement." What follows are the instructions to be
followed over and over (in a loop)--until zero is entered at the
keyboard.
8. TOTAL := TOTAL + DIGITS; {line 8}
"Perform addition." Clearly this is not the addition that you were
taught in school. It is, however, correct and precise in the
"grammar" of Pascal.
More accurately, this instruction reads: "Take the value found in
the memory area called TOTAL, add to it the value found in memory
area called DIGITS, and insert the sum back into the memory area
called TOTAL."
TOTAL no longer holds the value zero. Zero has been "erased."
Instead it holds the new value found by adding the old value to
whatever number that we entered at the keyboard.
9. readln(DIGITS); {line 9}
"Take another number from the keyboard and place it in the memory
area called DIGITS." The new number that we entered takes the
place of the number we entered previously.
10. end; {line 10}
"End of the loop." This is a form of punctuation. It concludes
the steps begun in statements 6 and 7. And in this case, it sends
the program running back to the while conditional. Because while
signals repetition, the computer returns to the instruction in
statement 6 and finds that it must perform another examination of
the number entered.
If at any time we enter 0 (zero), the computer will discover that
the condition defined in statement 6 (is not zero) has not been
met. Until we enter zero, the computer will continue to take
numbers, one number at a time, perform addition, juggle its
memory, and wait for still another number.
Recall that this circular repetition of one or more steps is
called a loop.
If we enter 0 (zero), the computer will break out of the loop
dictated by while and proceed to the next (and in this instance,
the last) step in the procedure.
11. writeln(TOTAL : 10 : 2); {line 11}
"Show the total that you computed." This last instruction tells
the computer to display the sum of all of the additions that it
has performed. More accurately, it displays the value that it
finds in the memory area called TOTAL, and that value is the sum
of the additions. A piece of the instruction, : 10 : 2, tells the
computer how to display the number--in this case, as a number
occupying the space of ten characters on the screen, with two
digits following the decimal point.
Displaying images on the video screen is also under the control of
the system processing unit.
12. END. {line 12}
"The program is done." This is the last mark of punctuation. This
tells the computer to stop the program.
There is one serious flaw in this program. If at any time we enter a
letter, or a string of letters, or a combination of numbers and letters,
instead of a number, the program will not know what to do. It has not
been told what to do. The program instructions specified only numbers as
input.
In the face of this confusion, the computer will bring the program to a
grinding halt. The program aborts on the error we made--entering a
letter instead of a number.
A programming engineer would include in this simple program very specific
instructions about what to do if the person using the program enters
something other than a number. Very likely a better-designed program
would detect such an error and display a message. Then it would suggest
an answer and wait patiently for another number and continue with the
additions.
________________________________________________________________________
|As cryptic as the Pascal language might seem, it is still too close to|
|everyday language for the computer to use efficiently. |
________________________________________________________________________
Computers truly understand only collections of ones and zeros. Before
this or any other program is ready for the computer's use, it must be
translated into the binary language that the computer understands, called
machine language. Other programs called compilers perform this
translation.
MPE/iX 5.0 Documentation