193
Chapter 5 Collision Theory and Logic
To better understand what an algorithm is and how these three components
combine to create an algorithm, a real-world example of an algorithm might
help. Look at the algorithm needed to wash your hair.
Directions: Wet hair, apply shampoo to hair, lather, rinse, and repeat.
After shampooing, apply conditioner to hair, lather, and rinse.
Computer program to wash hair:
DECLARE variables—objHair, objShampoo, objConditioner
DEFINE method subroutines—WET, APPLY, LATHER, RINSE
Predefined methods—LOOP
WET (objHair)
APPLY (objShampoo, objHair)
LATHER (objShampoo)
RINSE (objHair)
LOOP (1)
APPLY (objConditioner, objHair)
LATHER (objConditioner)
RINSE (objHair)
The algorithm shown to wash hair demonstrates all three components
of an algorithm. The structure of the computer program is the declaring of
variables, defining methods, and the syntax of how the variables are called by
the methods. The sequence is the order required to get the hair shampooed and
then conditioned. The steps must be followed in order to get the hair washed.
You cannot do the steps out of order or the process will not work. Finally,
iteration is demonstrated by the loop command. The instructions required that
it be washed twice and conditioned once. The loop was set to 1, which means
that the steps above the
LOOP
command will repeat one additional time. There
was no
LOOP
command for the set of instructions for the conditioner steps.
Without a
LOOP
command, those steps will not repeat and the algorithm ends.
Ethical Gamer
Plagiarism is a form of theft where you copy
somebody else’s material without permission.
Under copyright law, as soon as something is in
tangible form, it is automatically copyrighted. A
copyright protects the creator from others using
the material without permission. Just about
everything you see in print, all the music you
listen to, any images you see on television or movie screens, and almost all
information on the Internet is copyrighted. Even the computer code used to
create websites is copyrighted. This means if you copy any of this material
without the permission of the copyright holder, you have committed theft.