An algorithm is a set of steps you must complete in order to accomplish some task. For example, brushing your teeth requires that you:
The order of these events are important. For example if we spit as our first step instead of our last then after scrubbing our teeth we have a mouth full and no instructions as to what to do with it.
In addition, we need to be sure we're being specific enough with our instructions. The instruction "apply toothpaste to toothbrush" may seem somewhat obvious, but it does neglect some substeps that are necessary. If you look closely, we never opened the toothpaste! Also, how do we get the toothpaste out of the container? We need to be careful that we get all the correct, necessary steps in enough detail to make the algorithm work every time. That being said, we can, and often have to, make some assumptions about the level of previous knowledge.
Algorithms are an important part of computer science: They define the what a computer does and how it does it. As such, programmers have many ways of defining these algorithms visually because it allows us to translate the algorithms into our programming language of choice. We will be examining flowcharting in this course:
Flowcharts define the flow of a program from one step to the next, defining the algorithm with a few basic shapes that have particular meanings:
A terminator looks like a rounded rectangle. Its entire purpose is to tell us where the algorithm starts and where it ends. We denote this by putting "Start" and "End" in the middle of the terminator. To the right is an example of a blank terminator.
Process blocks are meant to represent actions performed by the computer that the user does not see. An example would be if the computer were adding 2 numbers together. The action performed is written inside of a rectangle. To the right is an example of a blank process block:
Decisions are how we create programs that have different results every time. Programs would be boring without them. Decisions are shown using a diamond. Within the diamond we write the condition, for example, x > 0. To the right is an example of a blank decision:
Communication with the user is done through input and output statements. Without this interaction a program simply runs and completes on its own without the user knowing about any progress or the result of the program. This symbol is used for two things:
Flowcharts use parallelograms for input and output. To the right is a blank input or output symbol:
Flow determines the order of the instructions. We represent flow using arrows that connect two shapes together:
IMPORTANT:
While designing algorithms, you may find that you have run out of space. In order to avoid having extra or long flow arrows, we use on-page references. These references are numbered in order to show the reader where the flow of the program goes after reaching the end of a page. The symbol used is a circle:
Pseudocode is a high-level description of an algorithm. It uses similar structures as actual programming languages, except that they read more like standard English to make it easier to understand. It is a very informal language that is used only to describe an algorithm.
Decisions are shown through:
if CONDITION then
ACTIONS
end if
if CONDITION then
ACTION 1
else
ACTION 2
end if
if CONDITION_1 then
ACTION 1
else if CONDITION_2 then
ACTION 2
end if
A CONDITION is some evaluable check that can be done based on data that is available.
ACTIONS are what you do if a CONDITION is true. If a CONDITION is not true, then the ACTION is not performed.
If we were to write some pseudocode for leaving your house in the morning, it may look something like this:
put books in bag
if lunch available then
put lunch in bag
end if
if is sunny then
put on sunglasses
else
put sunglasses in bag
end if
put on shoes
if is cold then
get winter jacket
else if is cool then
get light jacket
end if
if is raining then
get umbrella
else if is going to rain later then
get umbrella
end if
get bag
open door
exit home
lock door
We can see what this would look like as a flow chart: