Held Friday, February 4, 2000
Overview
Today's outline does not yet have an overview. Please
let me know if you think it should have one.
Question 8 for today's class: Describe unambiguously how to find the lowest grade in a list of grades.
Question 9 for Monday's class: Suppose you've been given two sets of instructions that solve the same problem. What criteria might you use to determine whether one is better than the other?
Notes
- The Dewdney reading was significantly less clear than I remembered it
being. Sorry. It should make more sense (at least partially) after
today's class.
- Read Dewdney 15 (Time and Space Complexity) for Monday.
- It would help me if you put blank lines between the paragraphs of your
email messages.
Contents
Summary
- Algorithm components
- Exercise: square roots
- Finding the smallest value in a list
- Handouts:
- As we saw in Wednesday's class, it is often better to express
algorithms in a high-level language.
- At the same time, we need to be careful to express our
algorithms unambiguously.
- We would also like to express algorithms concisely.
- Let's consider some basic techniques and ideas that make it easier to
work with algorithms.
- We will often number the steps of an algoirthm to make it easier to
talk about different parts.
- Some steps will have ``substeps''. We may use numbers or letters
when we ``number'' the substeps.
- Dewdney uses numbers for substeps.
- I prefer letters for substeps and roman numbers for subsubsteps.
- Since most algorithms deal with values of some sort, it helps to
have places (containers) to put those values.
- Computers use memory to store those values.
- It also helps to be able to name those containers.
- We call a named container a variable.
- There are two things you can do with variables:
- Get the value of a variable
- Change the value of a variable
- In some programming languages, you write the verbose
Set the value of variable to expression
- For example,
Set the value of x to 5
- Similarly,
Set the value of x to (the value of y plus 5)
- In many programming languages, we write the more concise
``variable = expression''
- For example,
x = 5
- Similarly,
x = y + 5
- As you may recall from Wednesday's class (or may know from following
recipes), many algorithms have special cases.
- We typically handle such special cases by writing something like:
if (condition) then
what to do if the condition holds
- We may also write
if (condition) then
what to do if the condition holds
otherwise
what to do if the condition does not hold
- Sometimes we will want to repeat a series of steps.
- The most basic form of repetition is for a fixed number of times.
- We will sometimes write
-
Repeat steps X through Y N times
- Using our numbering system, we might also write
-
Repeat the following substeps N times
- At times, it will be helpful to count the values between 1 and N
when repeating. In those cases, we will typically write
-
For variable = 1 to N ...
- At times, we will instead want to look at every value in a group. In
those instances we will write
-
For each variable in group ...
- At other times, we will not know the number of times to repeat some
steps. In such case we will write one of the following:
-
Repeat steps X through Y until condition
-
Repeat the following substeps until condition
- Today and next week, we will see some examples of the usage of these
ideas.
- You can do a lot with just variables and expressions (particularly
if you want to compute mathematical expressions)
- We'll use a simple Unix programming language called
bc
(basic calculator) for this example.
-
bc has two basic operations:
- You can set the value of a variable by typing
var = expression
- You can determine the value of a variable by typing
var
- Start
bc by typing bc in a dtterm
window.
- Pick a positive number and store it in variable
a.
- Store the square of
a in variable b.
- Print out
b.
- Store the value 1/3 in variable
c.
- Print out the value of
c.
- Why do you think
c has that value?
- Type
scale(20) and try setting and printing the value of
c again.
- What happened this time?
- We're going to run Newton's algorithm to compute the square root
of a number. We'll compute the square root of 7.
- Here's the algorithm:
- 1. Pick a positive number and store it in variable
a.
- 2. Store the square of
a in variable b.
- 3. Store the sum of
b and 7 in c.
- 4. Store two times
a in d.
- 5. Store the quotient
b/d in a.
- 6. Print out
a.
- 7. Repeat steps 1-6 four more times.
- 8. Store the square of
a in variable b.
- 9. Print out the value of
b.
- At the end, I'll ask everyone what value they got for a.
- You can quit
bc by typing control-D.
- What can we say about this algorithm?
- How might we express it more clearly?
- We'll conclude today's class by considering how to concisely and clearly
describe how to find the smallest value in a list.
- Spend a few minutes considering
your answers.
- Are there patterns you see?
- How might we simplify things?