Practice Problem 1: Algorithms
Objective: To write detailed algorithms for common tasks
Here's an example of a pseudo-code algorithm to buy the textbooks for a class, using
parameters for the course number and section number:
To buy the books for CS 111A Section 001, you would write:
BuyTextBooks(CourseNumber="CS 111A", SectionNumber="001");
To buy the books for English 1A Section 581, you would write:
BuyTextBooks(CourseNumber="ENGL 1A", SectionNumber="581");
Here is the generic algorithm to buy textbooks:
BuyTextBooks(CourseNumber, SectionNumber):
- Go to the college bookstore.
- Pick up a shopping basket.
- Find the section for textbooks.
- Locate the shelf labeled for CourseNumber and SectionNumber.
- Repeat for each required book for this course section:
- Determine the required book’s title and author.
- Pick up a copy of the required book and put it in your shopping basket.
- Repeat for each optional book for this course section:
- Determine the optional book’s title and author.
- Pick up a copy of the optional book.
- Look at optional book to decide if you want to buy it:
- If you want to buy the optional book:
- Put the optional book in your shopping basket.
- If you do not want to buy the optional book:
- Put the optional book back on the shelf where you found it.
- Go to the cash register.
- Pay for the book(s) in your shopping basket.
- Exit the bookstore with your books.
Using this example as a guide, your assignment is to write algorithms with 10-20 steps for each of
the following tasks. You may write your algorithms using pseudo-code, logic diagrams, flow charts, or any
method you wish.
- Making a sandwich
The customer should be given a choice of sandwiches: either
"peanut butter and jelly" or "ham and cheese". So your algorithm must show this choice,
and what happens in each cases (how each sandwich is made, step by step). Write this as a generic algorithm
with a parameter for the type of sandwich. So to make a Peanut Butter and Jelly sandwich, you would write:
MakeSandwich(type="Peanut Butter and Jelly");
To make a Ham and Cheese sandwich, you would write:
MakeSandwich(type="Ham and Cheese");
Your job is to write a generic algorithm to work for either kind of sandwich - so it will make the one
specified by the parameter.
- Washing the dinner dishes
Assume the dirty dishes are still on the table. So your
algorithm must involve some repetition of steps for each dish: How to clear and wash them all, one by one.
You need to show clearly which steps are repeated for each dish. No parameters are needed for this one.
You can turn in your homework on paper, or
upload it to Insight. (All future assignments must be uploaded to
Insight.)
Paper submissions may be slid under my office door (Batmale 462) if I'm not there.
Return to main CS 111A page