Solution to Practice Problem 1: Algorithms
There are many correct variations of these algorithms, and many correct ways of writing them. As long as
your steps are logical and clear, and achieve the desired results as described in the assignment statement,
that's what I care about.
Making a Sandwich:
MakeSandwich(type):
- Put 2 pieces of bread on counter.
- Take a spreading knife out of the drawer
- If type="Peanut Butter and Jelly":
- Use knife to spread peanut butter on one piece of bread.
- Use knife to spread jelly on the other piece of bread.
- Put the bread together into a sandwich (with the peanut butter and jelly sides facing each other).
- If type="Ham and Cheese":
- Use knife to spread mayonnaise on one piece of bread.
- Use knife to spread mustard on the other piece of bread.
- Put sliced cheese on one piece of bread.
- Put sliced ham on top of sliced cheese on that same piece of bread.
- Put the bread together into a sandwich (with the wet sides facing each other).
- Wrap the sandwich and give it to the customer.
Washing Dishes:
- Fill one side of the sink with hot water.
- Squirt some dish detergent into the water.
- Fill the other side of the sink with hot water.
- Repeat for each dirty dish:
- Carry the dish to the compost can.
- Scrape the dish into the compost.
- Put the dish into the soapy water.
- Rub the dish under water until clean.
- Rinse the dish in the clear water.
- Put the dish in the dish drainer to dry.
- Drain the water from both sides of the sink.
Return to main CS 111A page