Page

# 1.3 Algorithms

Completion requirements

**What is an Algorithm?**

Any time you need a plan which requires a series of steps to accomplish a task you have a type of algorithm. For the purposes of writing code, an algorithm must do do things. First,

__it must solve the problem or deliver the desired results__and second,

__it must be efficient__!

Algorithms are everywhere. A recipe for baking a cake is an algorithm, the steps you use to solve a math problem is an algorithm and the order in which you prepare breakfast is an algorithm. Even the routine you follow every morning getting ready for school is an algorithm.

Example:

1. Get the cereal bowl from the cupboard.

2. Get a spoon from the drawer.

3. Get the cereal from the pantry.

4. Get the milk from the fridge.

5. Put a measured amount of cereal in the bowl.

6. Put a measured amount of milk in the bowl.

7. Eat the cereal.

8. If you are still hungry go to point 5 and repeat.

Note that the instructions are very specific and we don't assume that
the computer knows anything. If we don't tell it where to find the
cereal bowl then it will not get one.2. Get a spoon from the drawer.

3. Get the cereal from the pantry.

4. Get the milk from the fridge.

5. Put a measured amount of cereal in the bowl.

6. Put a measured amount of milk in the bowl.

7. Eat the cereal.

8. If you are still hungry go to point 5 and repeat.

Prime Numbers

(click on the NEXT button to see the example).

Please watch this video introducing algorithms.

In order to write an effective program you must have a plan. An algorithm is the plan. Remember that as you saw in the video, an algorithm must do two things. First is must solve the problem. Second, it must be efficient. If the solution to the problem does not work in all cases, or if there is a shorter way to solve the problem, then the algorithm must be improved.

NOTE: Every program you write must be accompanied by the algorithm you used to plan the coding process.

Please have a look at the Guessing Game.