We can use algorithms to describe ordinary activities in our everyday life. For example, we can consider a recipe as an algorithm for cooking a particular food.
The algorithm is described in Steps 1-3. Our input is the specified quantities of ingredients, what type of pan we are using and what topping we want. The output is an Easy Pizza Brownie. Think about how you can translate the given recipe into pseudocode using what you have learned so far in class. Remember that you would like to make your algorithm as general as possible.
Here is an example of how to write Step 1. First, notice that there are three separate actions to perform: heat, grease, and place. We can consider each of the actions as individual subroutines. The first thing we are told to do is Heat oven to 350° F. We could define a subroutine
that always heats the oven to 350°. This subroutine is very specific, as we know that you can heat an oven to any of a wide range of temperatures. If at some time in the future you wanted to heat the oven to 400°, you would need to define a new subroutine to do so. A better idea is to define a subroutine to heat the oven that accepts as input a temperature:
By defining our subroutine this way, we can heat the oven to 350° by
calling the subroutine heat_oven with input temperature 350, that
Next we need to Grease 12'' pizza pan or 13'' by 9'' baking pan. We define a subroutine grease that accepts as input a type of pan:
Now we are told If using a disposable pizza pan, place on cookie sheet to bake. We define a subroutine place_on_cookie_sheet that accepts as input a type of pan:
Now that we have all our subroutines, we can put everything together. We still need to define some variables. Let my_pan denote the type of pan we are using and a boolean variable disposable that is true if we use a disposable pizza pan and false otherwise.
How complicated is it to describe how you get dressed? We will try to figure this out by designing an algorithm for putting on clothes.
Let's call our algorithm get_dressed(). Since getting dressed involves putting on various items of clothing, we'll need a subroutine put_on, that takes as input a variable of type clothing. Variables of type clothing may be any particular item of clothing available to you -- in other words, what's in your closet.
Suppose you have the following items in your closet:
Scenario 1: Suppose you know exactly what you want to wear: your blue jeans, belt, red shirt, white socks and sneakers. Then getting dressed is a simple sequential process. The only thing you need to worry about when designing your algorithm is the order in which to put on the items of clothing.
Scenario 2: Suppose you want to wear jeans, a tshirt and sandals. However, you know one pair of jeans is dirty. Also, you can't remember if you left your sandals at your friend's house, so you might have to wear sneakers. In this scenario getting dressed will involve a selection of clothing, depending on what items are dirty and if you can find your sandals. To determine if an item of clothing is dirty, we declare a subroutine is_dirty that takes as input a variable of type clothing and returns true if the item of clothing is dirty. Similarly, we define a subroutine is_missing that takes as input a variable of type clothing and returns true if we can't find the clothing we are looking for. Here is our algorithm:
Scenario 3: Usually we choose different types of clothing depending on the weather. In this scenario, we want our algorithm to take as input the temperature outside and select between three different outfits depending on whether it is cold, warm, or hot.
Do you put on the first thing you find?
We are assuming that you do in fact have a shirt in your closet, else you will (literally) spend forever getting dressed.
Do you try on several items before deciding what to wear?