Algorithms

Algorithms Flowcharts Pseudocode Examples Assignment

Solutions

An algorithm is the set of specific instructions to perform, in a specific order, to carry out a specified task.

For example, if you wanted to go skiing or snowboarding over a long weekend, what would you do to make it happen? You need to plan the tasks to make it work out. There are several major elements -- travel, food, lodging, money, equipment, to name a few. Would you drive there yourself, go with friends or family in their car, walk, take a bus, fly? Would you rent a condo, rent a hotel room, sleep on the floor of the lodge, stay with a friend, sleep in the car? Would you pack your own food, take only enough money to eat fast food, take a credit card to buy expensive meals? Would you rent equipment there, or take your own? There are a lot of choices and a lot of decisions to make, based on criteria like time, expense, personal choice, and so on.

Consider this situation: a friend is flying here to visit, and that friend needs to get to your house. There are multiple solutions or possible algorithms to accomplish that goal. Let's examine these four possibilities, which begin when the friend's plane has arrived at the airport.

All four of these algorithms accomplish exactly the same goal, but each algorithm does it in completely different ways. Each algorithm also has a different cost and a different travel time. Taking a taxi, for example, is probably the fastest way, but also the most expensive. Taking the bus is definitely less expensive, but a whole lot slower. You choose the algorithm based on the circumstances.

Back to Top

Flowcharts

It can help to graphically represent the solutions in a flowchart. Look at this flowchart example.

Phone Support flowchart

Most flowcharts use 3 basic symbols: an oval or lozenge for the start or end of a process; rectangles, which show instructions or actions; and diamonds, which show decisions that must be made. The symbols are connected by arrows that show the flow of the program control. In each symbol, you write down the action or decision for that step.

Think about the problem of a broken lamp. How would you figure out how to fix it?

Flowchart for fixing broken lamp

State the problem to be solved.

First, you might ask if the lamp is plugged in. If it is not, then the solution is to plug it in.

If the lamp was plugged in, then ask if the bulb is burned out. If it is, then the solution is to replace the bulb.

If neither of those issues were the case, then the solution is to buy a new lamp.

The flowchart graphically represents an algorithm.

Back to Top

Pseudocode

Pseudocode is the algorithm stated in sentences or phrases, in a readable format. It doesn't have any specific programming language in it. The description of the broken lamp process above might be considered pseudocode. Here is a better version.

If lamp is not plugged in, plug it in
Else if bulb is broken, replace it
Else buy a new lamp

This gives the programmer another way to consider all the options and posibilities, and can serve as documentation of the solution. It's usually a short and simple step to move from pseudocode to actual programming code. Writing pseudocode helps the programmer figure out what the form or program screen should look like, what data to ask for and display, and what tests to use.

How do you solve the problem of figuring out how many Euros you get for your American dollars? First, you need to know the formula for this calculation, which is the multiply the dollars by the conversion rate to get Euros. You could go online to look up the conversion rate to use in the program, or you could ask the user to provide a conversion rate. Would the user be happy with a program that asks for the conversion rate? Probably not, so you'll need to put the rate into the program. You will need the amount of money to convert. So the program will ask the user for the number of dollars to change to Euros, calculate the conversion using the rate provided in the program, and display the result. The pseudocode would look like this:

Look up conversion rate for program
Ask for dollars
Euros = conversion rate * dollars
Display Euros

You would consider this pseudocode to figure out if it's complete, if it will accomplish the task or solve the problem, if there are better ways to handle it. In this example, how do you ask for the dollars? You would probably get that information from the user with the program screen or form -- would you use a text box, radio buttons, check boxes, something else? The answers will be more and more obvious as you consider each part of the pseudocode.

Back to Top

Examples

Example 1

Examine the following flowchart and determine what it describes. What problem does it solve? The last step is to print a sum -- what will it print?

Flowchart

Example 2

Look at this flowchart for an algorithm to find the largest of three numbers. There is an error -- what is it?

Flowchart

Example 3

You need to write a program to convert temperatures from Fahrenheit to Celsius and from Celsius to Fahrenheit. Write the pseudocode for your algorithm.

First, you need to find out what the formulas are for these conversions. If you don't have the right calculations, the results of the program will be unusable. The formulas are:

CelsiusTemperature = 5/9 * (FahrenheitTemperature - 32)
FahrenheitTemperature = (9/5 * CelsiusTemperature) + 32

How will your program know which formula to use? The user needs to specify which conversion on the screen. And the user will need to provide the temperature to convert. The pseudocode might look like this:

Get type of conversion from user
Get Temperature to convert from user
If type of conversion is Fahrenheit-to-Celsius then
ConvertedTemperature = 5/9 * (Temperature - 32)
Else (type of conversion is Celsius-to-Fahrenheit)
ConvertedTemperature = (9/5 * Temperature) + 32
Display ConvertedTemperature

Consider if this algorithm will solve the problem. It looks like it will convert one number to another, using the formulas for temperature conversion, as requested. Is it possible for the user to provide a number that is not legitimate? You might want to limit the range, perhaps based on weather termperatures or body temperatures -- or not, perhaps any number is interesting. What if the user types letters instead of numbers? Since that wouldn't work, then perhaps you should add a check to verify that the input isn't letters. You should also consider if there are any other areas of the algorithm that might go wrong or need limitations.

Using this process, you know that you need to develop a program and form that asks the user to choose one of 2 types of conversions and to input a temperature. This will help you design the form. And you know you will need to display the result, another element to design into the form. The pseudocode helps figure out the algorithm and design the form. The algorithm now looks like this:

Get type of conversion from user (put 2 choices on screen, with radio buttons perhaps)
Get Temperature to convert from user
Check that Temperature is numbers, not letters (may also check for high and low range)
If type of conversion is Fahrenheit-to-Celsius then
ConvertedTemperature = 5/9 * (Temperature - 32)
Else (type of conversion is Celsius-to-Fahrenheit)
ConvertedTemperature = (9/5 * Temperature) + 32
Display ConvertedTemperature

Back to Top

Assignments

Project 1: Write the pseudocode to calculate the miles per gallon for the user's car. The user will provide the miles travelled and the gallons purchased.

Project 2: Write the pseudocode to calculate the sum of 5 numbers. The user will type in the numbers, which should range from 0 to 100.

Project 3: Write the pseudocode to find the average of 4 numbers entered by the user. The numbers should range from 0 to 100.

Back to Top