Prelab 2: Everyday Algorithms

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

heat_oven_to_350() /* a very specific 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:

heat_oven( temperature ) /* a better, generalized subroutine */

By defining our subroutine this way, we can heat the oven to 350° by calling the subroutine heat_oven with input temperature 350, that is, heat_oven(350). If we want to heat the oven to 400° we simply call the same subroutine with with input temperature 400: heat_oven(400).

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:

grease( 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:

place_on_cookie_sheet( 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.

heatOven( 350 ); grease( my_pan ); if( disposable == true ) { place_on_cookie_sheet( my_pan ); }
Problem 1: Design subroutines for Steps 2 and 3.

Algorithms for Getting Dressed

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:

blue_jeans black_jeans brown_pants black_pants
blue_shirt red_shirt sweatshirt sweater
sandals shoes sneakers boots
white_socks black_socks orange_socks wool_socks
shorts tshirt belt jacket

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.

get_dressed() { put_on( red_shirt ); put_on( blue_jeans ); put_on( belt ); put_on( white_socks ); put_on( sneakers ); }

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:

get_dressed() { put_on( tshirt ); if( is_dirty(blue_jeans) ) put_on( black_jeans ); else put_on( blue_jeans ); if( is_missing(sandals) ) put_on( sneakers ); else put_on( sandals ); }

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.

get_dressed( int temperature ) { if( temperature > 75 ) /* it's hot out */ put_on( tshirt ); put_on( shorts ); put_on( sandals ); else if( temperature > 60 ) /* it's warm out */ putOn( blue_shirt ); putOn( blue_jeans ); putOn( white_socks ); putOn( sneakers ); else /* it's kind of cold */ putOn( sweatshirt ); putOn( blue_jeans ); putOn( wool_socks ); putOn( shoes ); putOn( jacket ); }
Problem 2: Design an algorithm get_dressed() that describes how you decide what you want to wear. You can add as many new items of clothing to your closet as you want. You may need to declare new subroutines depending on how you choose what to wear. Some examples are below.

Do you put on the first thing you find?

If you just put on the first shirt you find in the closet, you might want to define a subroutine first_found that will return the first item of type clothing found in your closet. However, you also need some way to distinguish if the item of clothing is in fact a shirt. Define a second subroutine is_shirt that takes as input an item of clothing and returns true if it is a shirt. Then put these two subroutines put together in a while-loop: item = first_found(); while( !is_shirt(item) ) /* ! means "not" */ { item = first_found(); }

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?

You will need a subroutine take_off to take off the clothes you just put on. You also need a subroutine (or several) to test whether you will try on something else. Some examples could be too_small, too_big, smells_really_bad, makes_me_look_fat, etc, all of which should return either true or false.