CS221 Algorithm Basics. What is an algorithm? An algorithm is a list of instructions that transform...
-
Upload
antonia-baldwin -
Category
Documents
-
view
213 -
download
0
Transcript of CS221 Algorithm Basics. What is an algorithm? An algorithm is a list of instructions that transform...
CS221
Algorithm Basics
What is an algorithm? An algorithm is a list of instructions that
transform input information into a desired output. Each instruction is a finite set of steps which can be executed in a defined order on a deterministic machine. The execution of the instructions must terminate in a finite amount of time.
Finite number of instructions Executable instructions Finite amount of time
Example Algorithm
Algorithm: Square 1) Request a value for X 2) Compute the value S = X * X 3) Provide the computed S to the caller
How does this algorithm fit the definition?
Algorithm Interface
Every algorithm has an interface defined by Input information needed for the algorithm to
execute Output information produced by the algorithm
When creating an algorithm, you should clearly specify the algorithm interface.
Problem-Solving Phase
Analyze the problem and specify what the solution must do
Develop a general solution (algorithm) to solve the problem
Verify that your solution really solves the problem
Expressing an Algorithm
To express an algorithm, it is often useful to write out the steps in great detail using a spoken language.
Some algorithms are so simple that it is sufficient to express them using a high level language code.
Most algorithms should be expressed using pseudocode. Pseudocode is a mix of code-style formatting and
commands with spoken language words (example later)
A Good Design
Three Properties of a Good Design Shows a good understanding and some analysis
of the problem we are trying to solve. Lists inputs/starting point and outputs/desired result.
Shows a plan for a set of steps to get from the starting point to the desired result. Gives those steps in enough detail to TEACH them.
Offers enough information that a programmer could use it as a guide to write a program for solving the problem.
The Summation Problem
Problem You need to write a set of instructions that will determine the summation value of a given number. The answer is the sum of every number from 1 up to and including the given number. For example:
SummProgram( 4 ) = 10
because
1 + 2 + 3 + 4 = 10
Creating an Algorithm An algorithm is a way to instruct your computer
to solve a problem or perform a task. The best way to identify an algorithm for a given
problem is to outline the steps you would go through if you were asked to solve the problem
Creating an Algorithm First, identify the major checkpoints which must
be accomplished. Then, break each major checkpoint down into
simple sub-tasks which are easily translated into computer code.
This approach is known as “Top-Down Design” Begin at the top, with the big picture Fill in the details to correspond with the big picture
Main Algorithm
Ask the User for the Number Subprogram -> Get the Summation Value of
that number Report the Answer to the User
Summation Algorithm Set temporary answer to zero. Add the number to the temporary answer Subtract one from number Repeat steps 2 and 3 until the number is
decreased to zero
PseudoCode1) Ask User for Number2) Store Number3) Find the Summation Value for Number
1) Set Answer to Zero2) While Number is Greater Than Zero
1) Add Number to Answer2) Subtract One from Number
4) Report Answer
Verify Solution BEFORE Writing Computer Code
It is easier to identify and correct problems with your solution at this point when it is still in your own words!