**Algorithm Lesson**

ITF - Mr. Shultz

What is an algorithm?

If you use a computer or any computer-based device (like a cell phone or iPod), you're using algorithms all the time.

All computer programs, or applications (a.k.a. apps), are made using algorithms.

As you work through this lesson, be sure to fill in the Algorithm Worksheet.

Go to the next page to begin learning more.

OK, so what is an algorithm?

It is an ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time.

That's a mouthful!

But what does it mean?

It will be easier to understand if we break it into four parts:

- An ordered sequence
- Unambiguous and well-defined instructions
- Performs some task
- Halts in finite time

Let's see what each step means, using a task that many of us perform - making a peanut butter and jelly sandwich.

Let's look at the first part of our algorithm definition - an ordered sequence.

That's not so bad, is it?

Dictionary.com defines a sequence as "the following of one thing after another".

So, in our sequence, we follow steps to make a peanut butter and jelly sandwich, and follow one step with another step.

The fact that our sequence is ordered just means that the steps must be followed in order.

For example, when making a peanut butter and jelly sandwich, these steps must be completed in order:

- Take the lid off of the jar of peanut butter.
- Put your knife into the peanut butter.

If you do not complete those steps in order, you may find yourself trying to put your knife through the lid.

Not a good idea! You could ruin a good knife, and if you cut through the lid, your peanut butter will not stay fresh.

Let's look at the next part of of the algorithm definition.

Now, let's look at the second part of the algorithm definition - unambiguous and well-defined instructions.

To understand this, you need to know what unambiguous means.

Dictionary.com defines *ambiguous* as "open to or having several possible meanings or interpretations".

So, __un__*ambiguous* would mean that the step is NOT open to or having several meanings.

In a computer program each step must have only one meaning, or the program will not work properly.

Let's see what an *ambiguous* instruction might look like in our peanut butter and jelly example:

- Take the lid off of the jar.

This may be a good instruction if we were making a plain peanut butter sandwich, and there was only one jar involved.

But if you have a jar of peanut butter and a jar of jelly, which one would you open if you saw that step?

You wouldn't know which one to open, would you?

So, to make that step __un__ambiguous, let's make it well-defined:

- Take the likd off of the jar
*of peanut butter*.

Not hard at all - you just have to make sure to be clear when you write your steps, and provide enough information.

Time for some review! See if you can match the definition with the correct term. If you're not sure or get any wrong, it would be a good idea to go back and review.

Now that you've reviewed, let's break down the rest of that algorithm definition.

So far, we've seen that an algorithm is "an ordered sequence of unambiguous and well-defined instructions".

The rest of the definition says that an algorithm "performs some task and halts in finite time".

Let's look at the third part - performs some task.

In our example, the task is to make a peanut butter and jelly sandwich.

This may seem obvious, but you must always know what task you want to perform before you can start making an algorithm.

You may have heard the saying "begin with the end in mind".

That is good advice when creating an algorithm.

If you don't know that your task is to make a peanut butter and jelly sandwich, you could have all of the steps, but wouldn't know the correct order to put them in.

Let's finish off our algorithm definition on the next page.

The last part of the definition says that an algorithm "halts in finite time".

This simply means that you must decide when your algorithm halts.

You may say that the algorithm halts when the peanut butter and jelly sandwich is ready, but it may not be that simple.

For some people, the algorithm ends when the two slices of bread are put together.

For other people, the algorithm ends when the sandwich is sliced into two halves.

And will your algorithm end when the sandwich is done, or will it end after you have returned the peanut butter to the cupboard, the jelly to the refrigerator, the bread to the bread box, put the knife in the dishwasher, and cleaned the countertop?

All of these answers are acceptable, but you must be able to define when the algorithm ends so you can include all of the necessary steps.

We've now broken down the definition of an algorithm as "an ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time".

As you create computer algorithms, you should know the characteristics of algorithms.

Algorithms:

- Have a starting point
- Have Input
- Process something
- Have output
- Have a stopping point

Using our peanut butter and jelly sandwich example:

- A starting point - the peanut butter, jelly, bread, and knife are on the counter.
- Input - peanut butter, jelly, and bread.
- Processes something - puts peanut butter and jelly on bread and puts the two slices together.
- Has output - the sandwich.
- Has a stopping point - the sandwich is made, the peanut butter, jelly, and bread are put away, and the knife is in the dishwasher.

Now that we have a good idea of what an algorithm is, let's make one that will work on the computer.

What would an algorithm look like if you want to make a program to convert a Celsius temperature to Fahrenheit?

Let's start with the formula to make the conversion:

- Fahrenheit temperature = (Celsius temperature times 9, then divided by 5), then add 32

To shorten that:

- F will stand for Fahrenheit temperature
- C will stand for Celsius temperature
- * means to multiply
- / means to divide

So, our formula will look like this:

- F = (C * 9/5) + 32

But, how do we turn this formula into an algorithm?

To turn our temperature conversion formula into an algorithm, let's remember that algorithms:

- Have a starting point
- Have Input
- Process something
- Have output
- Have a stopping point

Let's make that even simpler:

- Start
- Input
- Process
- Output
- End

So, how does our formula work in this format? Let's see:

- Start
- Input - Get Celsius temperature
- Process - Multiply times 9
- Process - Divide by 5
- Process - Add 32
- Output - Display Fahrenheit temperature
- End

Are you surprised that there are three "Process" steps?

Don't be - the processing is what the algorithm is all about!

And don't forget the part of the algorithm definition that said "well-defined" instructions.

In this case, converting the temperature required us to multiply, divide, and add - and even a super-fast computer has to do those steps separately.

Time for another review! See if you can put the steps of the algorithm we just made into the correct order. If you can't, be sure to go back and review.

Now, let's try something similar - but different.

Now, we're going to do the temperature conversion the other way.

What would an algorithm look like if you want to make a program to convert a Fahrenheit temperature to Celsius?

Let's start with the formula to make the conversion:

- Celsius temperature = (Fahrenheit temperature minus 32) times 5, then divided by 9

To shorten that:

- F will stand for Fahrenheit temperature
- C will stand for Celsius temperature
- * means to multiply
- / means to divide

So, our formula will look like this:

- C = (F - 32) * 5/9

How do we turn this formula into an algorithm?

OK - you've seen what the first algorithm looked like.

And you've seen the second formula:

- C = (F - 32) * 5/9

Let's see if you can put the second algorithm in the correct order:

For review - the Fahrenheit to Celsius formula is:

- C = (F - 32) * 5/9

And the algorithm looks like this:

- Start
- Input - Get Fahrenheit temperature
- Process - Subtract 32
- Process - Multiply times 5
- Process - Divide by 9
- Output - Display Celsius temperature
- End

Now you've seen how some basic algorithms work.

In the next lesson, you will see how to create a flowchart.

Flowcharts are a visual way of looking at algorithms that can really help when algorithms get more complicated.

For now, make sure you have completed the Algorithm Worksheet, then follow any other instructions on the class web page.