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.
We input the ingredients, follow Steps 1-3, and output an Easy Pizza Brownie! Furthermore, Steps 1-3 can be viewed as three subroutines which may in turn be further divided broken into simpler subroutines. In Step 1, the first thing we are told to do is Heat oven to 350° F. This can be rewritten as a subroutine declaration by heatOven(temp), where temp is the input temperature value for the subroutine heatOven for heating up the oven. We can rewrite all of Step 1 as follows:
Problem 1: Design subroutines for Steps 2 and 3.
How complicated is it for people to get dressed? We will try to figure this out using what we learned in class about problem solving and algorithm design.
Let's call our algorithm GetDressed(). Since getting dressed involves putting on various items of clothing, we'll need a subroutine putOn(clothing), where putOn is the name of the subroutine, clothing is its input value, and the subroutine contains instructions for putting on the input item of clothing. The variable 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 of clothing 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.
Scenario 2: Suppose you want to wear jeans, a tshirt and sandals but one pair of jeans is dirty. Then getting dressed will involve a selection of clothing, depending on what items are dirty. To determine if an item of clothing is dirty, we declare a subroutine isDirty(clothing) that is true if the item of clothing is dirty and false otherwise.
Scenario 3: Suppose we have a subroutine int temperature() that gives us the temperature outside. We want to design an algorithm that selects between three different outfits depending on whether it is cold, warm, or hot outdoors.
Problem 2: Design an algorithm GetDressed() that describes how you decide what you want to wear. You can define new items of clothing if you want. You may need to declare new subroutines depending on the criteria that affect the outfit you put on. Here are some examples.
Do you put on the first thing you find?
Do you try on several items before deciding what to wear?
Reading (optional):